Skip to content

Operators

Go provides operators similar to C, with a few specifics.

  • Arithmetic: + - * / %
  • Comparison: == != < <= > >=
  • Logical: && || !
  • Bitwise: & | ^ &^ (AND NOT) << >>
  • Assignment: = += -= *= /= %= &= |= ^= <<= >>=

Use different operators.

package main
import "fmt"
func main() {
a, b := 10, 3
fmt.Println("Addition:", a+b)
fmt.Println("Integer division:", a/b)
fmt.Println("Remainder:", a%b)
// Logical operators
x, y := true, false
fmt.Println("x && y:", x && y)
fmt.Println("x || y:", x || y)
// Bitwise
c, d := 0b1100, 0b1010
fmt.Printf("c & d = %b\n", c&d) // 1000
fmt.Printf("c | d = %b\n", c|d) // 1110
fmt.Printf("c ^ d = %b\n", c^d) // 0110
fmt.Printf("c &^ d = %b\n", c&^d) // 0100
fmt.Printf("c << 1 = %b\n", c<<1) // 11000
}
Terminal window
Addition: 13
Integer division: 3
Remainder: 1
x && y: false
x || y: true
c & d = 1000
c | d = 1110
c ^ d = 110
c &^ d = 100
c << 1 = 11000