golang 运算符

目录

  • [1. golang 运算符](#1. golang 运算符)
    • [1.1. Types of Operators](#1.1. Types of Operators)
    • [1.2. Bitwise Operators (位运算符)](#1.2. Bitwise Operators (位运算符))
    • [1.3. Logical Operators (逻辑运算符)](#1.3. Logical Operators (逻辑运算符))
    • [1.4. Arithmetic Operators (算术运算符)](#1.4. Arithmetic Operators (算术运算符))
    • [1.5. Assignment Operators (赋值运算符)](#1.5. Assignment Operators (赋值运算符))
    • [1.6. Comparison Operators (比较运算符)](#1.6. Comparison Operators (比较运算符))

1. golang 运算符

1.1. Types of Operators

  1. Bitwise Operators (位运算符)
  2. Logical Operators (逻辑运算符)
  3. Arithmetic Operators (算术运算符)
  4. Assignment Operators (赋值运算符)
  5. Comparison Operators (比较运算符)

1.2. Bitwise Operators (位运算符)

go 复制代码
package main

import "fmt"

func main() {
    x := 3
    y := 5
	z := false
    // 3 -> 011
    // 5 -> 101
    fmt.Println("X AND Y = ", x & y)
    fmt.Println("X OR Y = ", x | y)
    fmt.Println("X EXOR Y = ", x ^ y)
    fmt.Println("X Right Shift 1  = ", x >> 1)
    fmt.Println("X Right Shift 2  = ", x >> 2)
    fmt.Println("Y Left Shift 1 = ", y << 1)
}
sh 复制代码
$ go run bitwise/main.go

X AND Y =  1
X OR Y =  7
X EXOR Y =  6
X Right Shift 1  =  1
X Right Shift 2  =  0
Y Left Shift 1 =  10

很多编程语言使用 ~ 作为一元按位取反(NOT)操作符, Go 重用 ^ XOR 操作符来按位取反:

go 复制代码
// 错误的取反操作
func main() {
    fmt.Println(~2) // bitwise complement operator is ^
}
 
// 正确示例
func main() {
    var d uint8 = 2
    fmt.Printf("%08b\n", d)     // 00000010
    fmt.Printf("%08b\n", ^d)    // 11111101
}

同时 ^ 也是按位异或(XOR)操作符:

go 复制代码
10000010 ^ 00000010 = 10000000

1.3. Logical Operators (逻辑运算符)

go 复制代码
package main

import "fmt"

func main() {
    a := 45
    b := "Something"
    fmt.Println(a > 40 && b == "Something")
    fmt.Println(a < 40 && b == "Something")
    fmt.Println(a < 40 || b == "Something")
    fmt.Println(a < 40 || b != "Something")
    fmt.Println(!(a < 40 || b != "Something"))
}
sh 复制代码
$ go run logical/main.go

true
false
true
false
true

1.4. Arithmetic Operators (算术运算符)

go 复制代码
package main

import "fmt"

func main() {
    a := 30
    b := 50
    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)
}
sh 复制代码
$ go run arithmetic/main.go
A + B =  80
A - B =  -20
A * B =  1500
A / B =  0
A % B =  30
go 复制代码
package main

import "fmt"

func main() {
    k := 3
    j := 20
    fmt.Println("k = ", k)
    fmt.Println("j = ", j)
    k++
    j--
    fmt.Println("k = ", k)
    fmt.Println("j = ", j)
}
sh 复制代码
$ go run arithmetic/main.go

k =  3
j =  20

k =  4
j =  19

1.5. Assignment Operators (赋值运算符)

go 复制代码
package main

import "fmt"

func main() {
    var a int = 100
    b := 20
    fmt.Println("a = ", a)
    fmt.Println("b = ", b)
    a += 30
    fmt.Println("a = ", a)
    b -= 5
    fmt.Println("b = ", b)
    a *= b
    fmt.Println("a = ", a)
    fmt.Println("b = ", b)
    a /= b
    fmt.Println("a = ", a)
    fmt.Println("b = ", b)
    a %= b
    fmt.Println("a = ", a)
    fmt.Println("b = ", b)
}
sh 复制代码
$ go run assignment/main.go

a =  100
b =  20

a =  130
b =  15

a =  1950
b =  15

a =  130
b =  15

a =  10
b =  15

1.6. Comparison Operators (比较运算符)

go 复制代码
package main

import "fmt"

func main() {
    a := 45
    b := 12
    fmt.Println("Is A equal to B ? ", a == b)
    fmt.Println("Is A not equal to B ? ", a != b)
    fmt.Println("Is A greater than B ? ", a > b)
    fmt.Println("Is A less than B ? ", a < b)
    fmt.Println("Is A greater than or equal to B ? ", a >= b)
    fmt.Println("Is A less than or equal to B ? ", a <= b)
}
sh 复制代码
$ go run comparison/main.go

Is A equal to B ?  false
Is A not equal to B ?  true
Is A greater than B ?  true
Is A less than B ?  false
Is A greater than or equal to B ?  true
Is A less than or equal to B ?  false
相关推荐
轻口味20 分钟前
命名空间与模块化概述
开发语言·前端·javascript
晓纪同学1 小时前
QT-简单视觉框架代码
开发语言·qt
威桑1 小时前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
飞飞-躺着更舒服1 小时前
【QT】实现电子飞行显示器(简易版)
开发语言·qt
明月看潮生1 小时前
青少年编程与数学 02-004 Go语言Web编程 16课题、并发编程
开发语言·青少年编程·并发编程·编程与数学·goweb
明月看潮生1 小时前
青少年编程与数学 02-004 Go语言Web编程 17课题、静态文件
开发语言·青少年编程·编程与数学·goweb
Java Fans2 小时前
C# 中串口读取问题及解决方案
开发语言·c#
盛派网络小助手2 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#
007php0072 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生2 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl