It's used for declaring and initializing variables in a concise and convenient way, inferring the variable's type from the assigned value on the right-hand side. This operator is unique to Go and is often used in Go code to improve readability and reduce verbosity
package main
import "fmt"
func main() {
// Short variable declaration and initialization
x := 10
fmt.Println(x)
// You can also declare and initialize multiple variables in a single line
a, b := 20, "hello"
fmt.Println(a, b)
}
It's important to note that :=
can only be used within functions or local scopes, not for Package Level Variables