Learn Golang in Day 4
大纲
- Go语言运算符
- 算术运算符
- 关系运算符
- 逻辑运算符
- 位运算符
- 赋值运算符
- 其他运算符
算术运算符
运算符 |
描述 |
实例 |
+ |
加法 |
1+2= |
- |
相减 |
3 |
* |
相乘 |
|
/ |
除法 |
|
% |
取模 |
|
++ |
自增 |
|
-- |
自减 |
|
package main
import "fmt"
func main() {
var a int = 10
var b int = 20
var c int
c = a + b
fmt.Printf("%d + %d = %d\n", a,b,c)
c = a - b
fmt.Printf("%d - %d = %d\n", a,b,c)
c = a * b
fmt.Printf("%d * %d = %d\n",a,b, c)
c = a / b
fmt.Printf("%d / %d = %d\n",a,b, c)
c = a % b
fmt.Printf("%d mod %d = %d\n", a,b,c)
c = a
a++
fmt.Printf("%d++ = %d\n", c, a)
c = a
a--
fmt.Printf("%d-- = %d\n", c, a)
}
关系运算符
运算符 |
描述 |
实例 |
== |
|
a == b |
!= |
|
a != b |
> |
|
a > b |
< |
|
a < b |
>= |
|
a >=b |
<= |
|
a <= b |
package main
import "fmt"
func main() {
var a int = 2
var b int = 12
if a == b {
fmt.Printf("a == b \n")
} else {
fmt.Println("a not equal to b")
}
}
逻辑运算符
- &&,||,!
| 运算符 | 描述 | 实例 |
|--------|------|------|
| && | | |
| ! | | |
位运算符
指针运算符
package main
import "fmt"
func main() {
var a int = 4
var b int32
var c float32
var ptr *int
fmt.Printf("a 的值 %d\n", a)
fmt.Printf("a 的地址 %0x", &a)
}