@mrbourne
2019-07-05T07:54:51.000000Z
字数 4426
阅读 992
未分类
Go是静态类型的语言,不能在运行期间改变变量类型。
使用var定义变量,自动初始化为零值。若提供初始值,可以省略变量类型,由编译器自动判断。
var x intvar f float = 1.5var s = "abc"
在函数的内部,可用更简略的“:=”方式定义变量
func main(){x := 123 //这里要注意是定义新的局部变量,还是修改全局变量。该方式容易出错。}
可以一次定义多个变量
var x, y, z intvar s, n = "abc", 123var (a intb float)func main(){n, s := 0x1234, "Hello, World!"println(x, s, n)}
多变量赋值时,先计算所有相关值,再从左向右一次赋值。
data, i := [3]int{0, 1, 2}, 0i, data[i] = 2, 100 // (i = 0) -> (i = 2), (data[0] = 100) 先计算了i的值为0,而非 data[2] = 100
特殊只写变量“_”, 用于忽略值占位。
func test() (int, string){return 1, "abc"}func main(){_, s := test()println(s)}
注意重新赋值与定义新同名变量的区别。
func main(){s := "abc"println(&s)s, y := "hello", 20 // 重新赋值:与前s在同一命名空间,且有变量定义。{s, z := 1000, 30println(&s, z) // 定义新的同名变量,与前s不在同一命名空间}}
常量值必须是编译器可确定的数字、字符串、布尔值。
const x, y int = 1, 2 // 多常量初始化const s = "Hello, World !" // 类型判断const ( // 常量组a, b = 10, 100c bool = false)func main(){const x = "xxx" // 未使用的局部常量不会引发编译错误}
不支持1UL(无符号长整型)、2LL(??)这样的类型后缀。
常量组中,如不提供类型和初值,则视作与上一常量相同。
const (s = "abc"x // x = "abc")
常量值还可以是len, cap (返回切片分配空间的大小), unsafe.Sizeof等编译器即可确定结果的函数返回值。
const (a = "abc"b = len(a)c = unsafe.Sizeof(b))
如果常量类型足以存储初值,则不会引发溢出错误。
const (a byte = 100 // int to byteb int = 1e20 // float64 to int , overflows)
枚举
关键字iota定义常量组中从0开始按行计数的自增枚举值。
const (Sunday = iota // 0Monday // 1 即 Monday = iotaTuesday // 2Wednesday // 3Thursday // 4Friday // 5Satauday // 6)const (_ = iota // iota = 0KB int64 = 1 << (10 * iota) // iota = 1 1左移10位 即 2^10 1024MB // 与KB表达式一样, 但iota = 2 1左移20位 即 2^20GBTB)
在同一常量组中,可以提供多个iota, 它们各自增长。
const (A, B = iota, iota << 10 // 0, 0 << 10C, D // 1, 1 << 10)
如果iota自增被打断,须显式恢复。
const (A = iota // 0B // 1C = "c" // cD // c, 与上一行相同E = iota // 4, 显式恢复。 注意计数包含了 C、D 两行。F // 5)
可通过自定义类型来实现枚举类型限制。
type Color intconst (Black Color = iota // 枚举类型为ColorRedBlue)func test(c Color) {}func main() {c := Blacktest(c)x := 1test(x) // Error: cannot use x (type int) as type Color in function argumenttest(1) // 常量会被编译器自动替换。}
更明确的数字类型命名,支持Unicode, 支持常用的数据结构。
| 类型 | 长度 | 默认值 | 说明 |
|---|---|---|---|
| bool | 1 | false | |
| byte | 1 | 0 | uint8 |
| rune | 4 | 0 | Unicode Code Point, int32 用与String数组的切片,存储Unicode字符 |
| int, uint | 4 或 8 | 0 | 32位或64位 |
| int8, uint8 | 1 | 0 | -128 ~ 127, 0 ~ 255 |
| int16, uint16 | 2 | 0 | -32768 ~ 32767, 0 ~ 65535 |
| int32, uint32 | 4 | 0 | -21亿 ~ 21亿, 0 ~ 42亿 |
| int64, uint64 | 8 | 0 | 相当大 |
| float32 | 4 | 0.0 | |
| float64 | 8 | 0.0 | |
| complex64 | 8 | 64位复数 | |
| complex128 | 16 | 128位复数 | |
| uintptr | 4 或 8 | 足以存储指针的uint32 或 uint64 整数 | |
| array | 值类型 | ||
| struct | 值类型 | ||
| string | "" | UTF-8字符串 | |
| slice | nil | 引用类型 | |
| map | nil | 引用类型 | |
| channel | nil | 引用类型 | |
| interface | nil | 接口 | |
| function | nil | 函数 |
支持八进制、十六进制,以及科学计数法。标准库math定义了各数字类型取值范围。
a, b, c, d := 071, 0xF, 1e9, math.MinInt16 // 0开头 八进制 0x开头 十六进制
空指针值nil, 而非NULL。
引用类型包括 slice、 map 和 channel。它们有复杂的内部结构,除了申请内存以外,还需要初始化相关属性。
内置函数new计算类型大小,为起分配零值内存,返回指针。
而make会被编译器翻译为具体的创建函数,由其分配内存和初始化成员结构,返回对象而非指针。
a := []int{0, 0, 0} // 提供初始化表达式a[1] = 10b := make([]int, 3) // makesliceb[1] = 10c := new([]int)c[1] = 10 // Error: invalid operation: c[1] (index of type *[]int)
不支持隐式类型转换,即便是从窄转宽(不丢失精度)也不行。
var b byte = 100// var n int = b // Error: cannot use b (type byte) as type int in assignmentvar n int = int(b) // 显示转换 窄向宽
使用括号避免优先级错误。
*Point(p) // 相当于 *(Point(p)) 将p地址先转为Point类型,再取Point指向的值(*Point)(p) // 将p转化为*Point指向的东西<-chan int(c) // 相当于 <-(chan int(c))(<-chan int)(c)
同样不能将其他类型当bool值使用。
a := 100if a { // Error: non-bool a (type int) used as if conditionprintln("true")}
字符串是不可变值类型,内部用指针指向UTF-8字节数组。
runtime.h
struct String{byte* str; // 指向byte类型数据的指针intgo len;}
使用索引号访问字符(byte)
s := "abc"println(s[0] == '\x61', s[1] == 'b', s[2] == 0x63) // true, true, true
使用“`”定义不做转义处理的原始字符串,支持跨行。
s := `ab\r\n\x00c`println(s)
输出
ab\r\n\x00c
连接跨行字符串时,“+”必须在上一行的末尾,否则引发编译错误。
s := "Hello," +"World!"s2 := "Hello,"+ "World!" //Error: invalid operaion: + untyped string
支持用两个索引号返回子串。子串依然指向原字节数组,仅修改了指针和长度属性。
s := "Hello, world!"s1 := s[:5] // Hellos2 := s[7:] // world!s3 := s[1:5] // ello
单引号字符常量表示 Unicode Code Point, 支持 \uFFFF、\U7FFFFFFF、\xFF 格式。对应 rune 类型, UCS-4。
func main(){fmt.Printf("%T\n", 'a') // %T 类型var c1, c2 rune = '\u6211', '们'println(c1 == '我', string(c2) == "\xe4\xbb\xac")}
输出
int32 // rune 是 int32的别名true true
要修改字符串,可先将其转换为[]rune或[]byte, 完成后再转换为string。无论哪种方式,都会重新分配内存,并复制字节数组。
func main(){s := "abcd"bs := []byte(s)bs[1] = 'B'println(string(bs))u := "电脑"us := []rune(u)usp[1] = '话'println(string(us))}
输出:
aBcd电话
用for 循环遍历字符串时,也支持 byte 和 rune 两种方式。
func main(){s := "abc汉字"for i := 0; i < len(s); i++ { //bytefmt.Printf("%c", s[i])}fmt.Printlnfor _, r := range s { //runefmt.Printf("%c", r)}}
输出:
a,b,c,,æ,±,,å,-,,a,b,c,汉,字
支持指针类型 *T, 指针的指针 **T, 以及包含包名前缀的 *.T
func main {type data struct { a int }var d = data{1234}var p *datap = &dfmt.Printf("%p, %v\n", p, p.a) //直接用指针访问目标对象成员,无须转换。}
输出:
0x2101ef018, 1234
不能对指针做加减法运算。
x := 1234p := &xp++ // Error: invalid operation: p += 1 (mismatched types *int and int)
可以在 unsafe.Pointer 和任何类型指针间进行转换。
func main(){x := 0x12345678p := unsafe.Pointer(&x) // *int -> Pointern := (*[4]byte)(p) // Pointer -> *[4]bytefor i := 0; i < len(n); i++ {}}