Operators
Context
Section titled “Context”Go provides operators similar to C, with a few specifics.
Categories
Section titled “Categories”- Arithmetic:
+-*/% - Comparison:
==!=<<=>>= - Logical:
&&||! - Bitwise:
&|^&^(AND NOT)<<>> - Assignment:
=+=-=*=/=%=&=|=^=<<=>>=
Example
Section titled “Example”Use different operators.
Code example
Section titled “Code example”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}Output
Section titled “Output”Addition: 13Integer division: 3Remainder: 1x && y: falsex || y: truec & d = 1000c | d = 1110c ^ d = 110c &^ d = 100c << 1 = 11000