Assignment Operators
- Assignment operators are used to assign values to variables.
- In the following example, the assignment operator (=) is used to assign the value 10 to the variable x:
Example:
package main
import "fmt"
func main() {
var x = 10
fmt.Println(x)
}
- This code declares a variable x, assigns it the value 10, and prints the value to the console.
Addition Assignment Operator (+=)
- The += operator adds a specified value to an existing variable and updates the variable with the new value.
Example:
package main
import "fmt"
func main() {
x := 10
x += 5 // Same as: x = x + 5
fmt.Println(x)
}
Output:
15