Frequently asked questions


How can I use a string template for string formatting?

Use fmt.Sprintf.

func TimeToStr(t time.Time) string {
    return fmt.Sprintf("%d/%02d/%02d", t.Year(), t.Month(), t.Day())

How do I get the size of an open file?

statInfo, err := f.Stat()
if err != nil {
        return 0, err
fileSize := statInfo.Size()

Size() returns an int64, you need to cast other values you want to operate on to int64:

blockCapacity := statInfo.Size()/int64(len(byteStr))

When do you use pointers in Go?

In Go function parameters are passed as values, i.e the function receives a copy of the variable whose scope is limited to the function. This means that any modification made to the variable does not have any effect on the variable you pass to the function call. If you want to use a function to change the value of a variable you can do this by passing a pointer of the variable to the function.