数组与字符串为值类型,切片、映射、通道为值类型,赋值需注意。
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 //数组 9 a1 := [3]int{1, 2, 3} 10 a2 := a1 11 a2[0] = 2 12 fmt.Println(a1) //print: 1 2 3 13 fmt.Println(a2) //print: 2 2 3 14 // 字符串 15 s1 := "123" 16 s2 := s1 17 s2 = "223" 18 fmt.Println(s1) //print 123 19 fmt.Println(s2) //print 223 20 // 切片 21 slice1 := []int{1, 2, 3} 22 slice2 := slice1 23 slice2[0] = 2 24 fmt.Println(slice1) //print 2 2 3 25 fmt.Println(slice2) //print 2 2 3 26 // map 27 m1 := make(map[int]string) 28 m1[1] = "1" 29 m2 := m1 30 m2[1] = "2" 31 fmt.Println(m1) //print 1:2 32 fmt.Println(m2) //print 1:2 33 // chan 34 c1 := make(chan bool) 35 c2 := c1 36 go func() { 37 v := <-c2 38 fmt.Println(v) //print true 39 }() 40 c1 <- true 41 }
byte为uint8类型,string不是[]byte或[]rune类型
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 s := "hello world" 9 var i interface{} = s 10 11 switch i.(type) { 12 case []byte: 13 fmt.Println("string type is []byte") 14 case []rune: 15 fmt.Println("string type is []rune") 16 case []int8: 17 fmt.Println("string type is []int8") 18 } 19 20 slice := []byte{1, 2} 21 i = slice 22 switch i.(type) { 23 case []uint8: 24 fmt.Println("byte is uint8")//print 25 } 26 }
接口为类型和值的二元组,nil类型为空且值为空
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func trueNil() interface{} { 8 return nil 9 } 10 11 func falseNil() interface{} { 12 var ret *int = nil // 此nil带有类型 13 return ret 14 } 15 func main() { 16 if trueNil() == nil { 17 fmt.Println("trueNil") //print trueNil 18 } 19 20 if falseNil() == nil { 21 fmt.Println("falseNil") //no print 22 } 23 }
有疑问加站长微信联系(非本文作者)