Unlike Python, where JavaScript can be used without defining parameter types, Go is strongly typed, meaning that when you use a parameter you need to declare it and then assign it to it

example

package main

import "fmt"

func main(a) {
    var a = "initial"
    fmt.Println(a)

    var b, c int = 1.2
    fmt.Println(b, c)

    var d = true
    fmt.Println(d)

    var e int
    fmt.Println(e)

    f := "apple"
    fmt.Println(f)
}
Copy the code

parsing

var a = "initial"
fmt.Println(a)
Copy the code

Var is used for variable definition

var b, c int = 1.2
fmt.Println(b, c)
Copy the code

You can also define more than one variable at a time

var d = true
fmt.Println(d)
Copy the code

The GO language deduces the type of a variable from the initialized value

var e int
fmt.Println(e)
Copy the code

If a variable is not initialized when it is defined, go automatically assigns it zero-valued, and the zero value of int is 0

f := "apple"
fmt.Println(f)
Copy the code

:= syntax is an abbreviation for definition plus initialization assignment, and the above code is equivalent to

var f string = "apple"
Copy the code

The results

$ go run variables.go
initial
1 2
true
0
apple
Copy the code