var i int // Simply declare variable
var i int = 42 // Declare and assign variable
i := 42 // Infer variable type from value
fmt.Printf("%v, %T\n", i, i) // Print value and typeVariables can be declared outside of functions, but then the type needs to be specified explicitly.
They can also be declared in groups.
var (
name string = "Daniel"
favouriteNumber int = 9
homeURL string = "https://dangarmol.dev/"
)This is valid code:
var i int = 35
func main() {
var i int = 42
i = 23
}This is not:
var i int = 35
var i int = 42 // This will fail
i := 23 // This will fail tooWhen declaring a variable at the package level, if its name stars with a lowercase letter, it is exposed within the package only, whereas if it is in uppercase, it is exposed publicly outside the package. This also applies to functions.
i := 42
f := 4.2
a = float32(i) // Works fine
b = int(f) // Works, but loses information
c = string(i) // This will convert to unicode char 42! -> '*'
d = strconv.Itoa(i) // Import "strconv"