Master Go Operators: From Arithmetic to Pointers with Practical Examples
This guide explains Go's various operators—including arithmetic, assignment, relational, logical, bitwise, and pointer operators—provides clear code samples for each type, demonstrates their output, and clarifies how they behave in different scenarios.
1 Operator Classification
1 算数运算符:+,-,*,/,%,++,--
2 赋值运算符:=,+=,-=,*=,/=,%=
3 关系运算符:==,!=,<,>,<=,>=
4 逻辑运算符:&&,||,!
5 位运算符:&,|,^,<<,>>
6 其他运算符:&,*2 Arithmetic Operators
2.1 "+" Example
package main
import "fmt"
func main() {
// "+" can denote a positive number
var x = +4
fmt.Println(x)
// "+" can perform addition
var y = 3 + 4
fmt.Println(y)
// string concatenation using "+"
var str1 = "hello" + " word!"
fmt.Println(str1)
} 4
7
hello word!2.2 "/" Example
package main
import "fmt"
func main() {
// Integer division yields an integer result
var x = 10 / 3
fmt.Println(x)
// Floating‑point division yields a float result
var y = 10.00 / 3
fmt.Println(y)
} 3
3.33333333333333352.3 "%" Example
package main
import "fmt"
func main() {
// Modulo operation works with integers
var x = 15 % 6
fmt.Println(x)
fmt.Println(-15 % 6)
fmt.Println(-15 % -6)
fmt.Println(15 % -6)
} 3
-3
-3
32.4 "++" Example
package main
import "fmt"
func main() {
// ++ increments, -- decrements; must appear after the variable
var a int = 5
a++
fmt.Println(a)
a--
fmt.Println(a)
} 6
53 Assignment Operators
3.1 "+=" and "/=" Example
package main
import "fmt"
func main() {
var a int = 5
a += 3 // equivalent to a = a + 3
fmt.Println(a)
a /= 4 // equivalent to a = a / 4
fmt.Println(a)
} 8
24 Relational Operators
Relational operators always return a bool value (true or false).
They are commonly used in control‑flow statements.
4.1 Example
package main
import "fmt"
func main() {
var a int = 5
b := 6
fmt.Println("a =", a, "; b =", b)
fmt.Println("a > b", a > b)
fmt.Println("a < b", a < b)
fmt.Println("a == b", a == b)
fmt.Println("a != b", a != b)
fmt.Println("a <= b", a <= b)
fmt.Println("a >= b", a >= b)
} a = 5 ; b = 6
false
true
false
true
true
false5 Logical Operators
5.1 Example
package main
import "fmt"
func main() {
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(true && true)
fmt.Println(!true)
} false
true
true
false6 Bitwise Operators
Bitwise operators work on the binary representation of integer values. They are fast and often used in low‑level programming.
6.1 Logical Bitwise Operators
& — both bits are 1 → result 1 (AND)
| — at least one bit is 1 → result 1 (OR)
^ — bits differ → result 1 (XOR)6.2 Shift Operators
<< shifts bits left, filling with 0s; >> shifts bits right, preserving the sign for signed integers.
package main
import "fmt"
func main() {
var a int8 = +85
var b int8 = -43
fmt.Println(a << 3)
fmt.Println(a >> 3)
fmt.Println(b >> 3)
} -88
10
-67 Pointers – Derived Types
7.1 Basic Introduction
1. A variable represents a memory region with a name, type, and value.
2. A pointer variable stores the address of another variable.
3. Declaration example: var ptr *int = &someVariable8 Other Operators (& and *)
1. & returns the address of a variable.
2. * dereferences a pointer to obtain the value stored at that address.8.1 Example
8.1.1 Viewing Address and Value
package main
import "fmt"
func main() {
var a int = 8
var ptr *int = &a
fmt.Println("Address of a:", ptr)
fmt.Println("Value at ptr:", *ptr)
} Address of a: 0xc000012088
Value at ptr: 88.1.2 Modifying a Variable via Pointer
package main
import "fmt"
func main() {
var a int = 9
fmt.Printf("a address= %v
", &a)
var ptr *int
ptr = &a
*ptr = 10
fmt.Println(a)
fmt.Printf("a address= %v
", &a)
} a address= 0xc000012088
10
a address= 0xc000012088Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
