In Go, you can declare and initialize variables in several ways. Here are a few examples:
- Declare a variable with a default initial value:
var x int // x is initialized to the default value of 0
- Declare a variable and initialize it with a value:
var x int = 42 // x is initialized with the value of 42
- Use type inference to declare and initialize a variable:
x := 42 // x is declared and initialized with the value of 42
- Declare and initialize multiple variables in a single statement:
var x, y, z int = 1, 2, 3 // x, y, and z are initialized with the values of 1, 2, and 3, respectively
- Use type inference to declare and initialize multiple variables in a single statement:
x, y, z := 1, 2, 3 // x, y, and z are declared and initialized with the values of 1, 2, and 3, respectively
Note that Go is a statically-typed language, which means that variables have a fixed type that cannot be changed during runtime.