Q:

When to use var or := in Go?

Is there any difference between the following two examples?
type Example struct {}

func main() {
  e := Example{}
}

vs.
type Example struct {}

func main() {
  var e Example
}

Is there a preferable one?
Thanks!

A:

Might worth noting:
Use := when you need to create a variable with a defined value on it.
number := 12
obj := SomeStruct{name: "user"}
slice := []string{"a", "b", "c"}
someNilPointerData := (*SomeStruct)(nil)

Use var keyword when you need to define a variable without any initialisation, so the zero value will be used on it.
var a int // zero value of int is 0
var mut sync.Mutex
var result []map[string]interface{}

Another advantage of using var keyword, creating multiple variables with same type in single statement.
var result1, result2, result3, result4 []map[string]interface{}

The var keyword is also useful to store certain literal value in a variable with different data type.
// store string literal value in interface{} variable
var anyValue interface{} = "hello world"

// store int literal value in float64 variable
var otherValue float64 = 12

