go--常量&运算符

常量const

1、常量声明:

const (
    a = 2
    b
    c
)
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
// ======
2
2
2

====:

2
2
2

2、并行声明常量:

const (
    a, b= 2, "A"
    c, d
)
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)

// ===
2
A
2
A

3、枚举iota

const (
    a = "A"  // 可以理解为,先赋值计数器为 0,又重新赋值 "A", 注意:组的第一行一定要有赋值表达式
    b             // 可以理解为,先赋值计数器为 1,又按照组内上一行的表达式,重新赋值 "A"
    c = iota  // 赋值计数器为 2
    d         // 赋值计数器为 3

)
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)

运算符

go的运算符都是从左至右结合

优先级(从高到低):

^   !     // 一元运算符

*   /    %    >>    <<    &   &^
+   -    |     ^       // 二元运算符

=    !=     <   <=     >    >=   

->       // 专门用于channel

&&

|| 

位移运算符:左移:<<     右移: >>

fmt.Println(1 << 10)   // 左移10位, 二进制位的运算

// 1024

go圣经传送门:https://books.studygolang.com/gopl-zh/ch3/ch3-01.html

猜你喜欢

转载自www.cnblogs.com/zhzhlong/p/9461485.html