Private Repository

Not all packages are public but go mod kind of expects that. There is a way around it.

  1. Modify .gitconfig (convince git to use ssh (and its key) instead of https)
[url "ssh://git@git.example.com/"]
        insteadOf = https://git.example.com/
  1. Set these environment variables, so go knows to not verify checksums for the private repo against the public checksum API; not use the go package proxy


Type go tool dist list or check Go (Golang) GOOS and GOARCH · GitHub

String formatting cheat sheet

fmt.Printf formatting tutorial and cheat sheet

String formatting with padding (Christopher Oezbek CC BY-SA 4.0)

Use the Printf function from the fmt package with a width of 6 and the padding character 0:

fmt.Printf("%06d", 12) // Prints to stdout '000012'

Setting the width works by putting an integer directly preceeding the format specifier ('verb'):

fmt.Printf("%d", 12)   // Uses default width,                          prints '12'
fmt.Printf("%6d", 12)  // Uses a width of 6 and left pads with spaces, prints '    12'

The only padding characters supported by Golang (and most other languages) are spaces and 0:

fmt.Printf("%6d", 12)   // Default padding is spaces, prints '    12'
fmt.Printf("%06d", 12)  // Change to 0 padding,       prints '000012'

It is possible to right-justify the printing by prepending a minus -:

fmt.Printf("%-6d", 12)   // Padding right-justified, prints '12    '

Beware that for floating point numbers the width includes the whole format string:

fmt.Printf("%6.1f", 12.0) // Prints '0012.0' (width is 6, precision is 1 digit)

It is useful to note that the width can also be set programmatically by using * instead of a number and passing the width as an int parameter:

myWidth := 6
fmt.Printf("%0*d", myWidth, 12) // Prints '000012' as before

This might be useful for instance if the largest value you want to print is only known at runtime (called maxVal in the following example):

myWidth := 1 + int(math.Log10(float64(maxVal)))
fmt.Printf("%*d", myWidth, nextVal)

Last, if you don't want to print to stdout but return a String, use Sprintf also from fmt package with the same parameters:

s := fmt.Sprintf("%06d", 12) // returns '000012' as a String