Top-level Variable Declarations

At the top level, use the standard var keyword. Do not specify the type,unless it is not the same type as the expression.

BadGood
  1. var _s string = F()
  2.  
  3. func F() string { return "A" }
  1. var _s = F()
  2. // Since F already states that it returns a string, we don't need to specify
  3. // the type again.
  4.  
  5. func F() string { return "A" }

Specify the type if the type of the expression does not match the desired typeexactly.

  1. type myError struct{}
  2.  
  3. func (myError) Error() string { return "error" }
  4.  
  5. func F() myError { return myError{} }
  6.  
  7. var _e error = F()
  8. // F returns an object of type myError but we want error.