How do I write the equivalent of Dim varName as int64 = value
in Go?
Whenever I find myself needing to declare a variable in Go I google a lot until I find the correct syntax.
How do I write the equivalent of Dim varName as int64 = value
in Go?
Whenever I find myself needing to declare a variable in Go I google a lot until I find the correct syntax.
Using a variable declaration (can be used inside functions and at the top level to create global variables):
var i int64 = value
If value
is a typed int64
value, you may omit the type which will be inferred:
var i2 = value // type is inferred
Short variable declaration (may only appear inside function bodies):
i3 := value // type is inferred
Notes:
Both with the variable declaration (without type) and with short variable declaration care must be taken if you use a value with different type - or an untyped constant. In these cases explicit type conversion may be required in order for the new variable to get the proper int64
type!
var i4 = 4 // Wrong! i4 will be of type int
var i5 = int64(4) // Good! i5 will be of type int64
var i6 int64 = 4 // Also good: explicitly provided type
i7 := 4 // Wrong! i4 will be of type int
i8 := int64(4) // Good! i5 will be of type int64