Go Operators
An operator is a symbol that tells Go to do some work with values or variables, such as adding them together.
A familiar one is +, which gives you the sum of two numbers:
Example
Prompt
Add two numbers together using the + operator and print the result.
Output: Go program and captured output
main.go
package main
import ("fmt")
func main() {
var a = 15 + 25
fmt.Println(a)
}
Example
The + operator isn't limited to plain numbers. Each side can be a number or a variable, so you can mix the two or sum two variables:
Prompt
Use the + operator to add values and variables together in multiple steps. Print the final sum.
Output: Go program and captured output
main.go
package main
import ("fmt")
func main() {
var (
sum1 = 100 + 50 // 150
sum2 = sum1 + 250 // 400
sum3 = sum2 + sum2 // 800
)
fmt.Println(sum3)
}