Local Variable Declarations

Short variable declarations (:=) should be used if a variable is being set tosome value explicitly.

BadGood
  1. var s = "foo"
  1. s := "foo"

However, there are cases where the default value is clearer when the varkeyword is use. Declaring Empty Slices, for example.

BadGood
  1. func f(list []int) {
  2. filtered := []int{}
  3. for , v := range list {
  4. if v > 10 {
  5. filtered = append(filtered, v)
  6. }
  7. }
  8. }
  1. func f(list []int) {
  2. var filtered []int
  3. for , v := range list {
  4. if v > 10 {
  5. filtered = append(filtered, v)
  6. }
  7. }
  8. }