go语言细节

1 数组与字符串为值类型,切片、映射、通道为值类型,赋值需注意。

 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 }
View Code

2 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 }
View Code

3 接口为类型和值的二元组,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 }
View Code

 4 slice,map操作多协程需要加锁。

 1 package main
 2 
 3 import (
 4     "math/rand"
 5     "sync"
 6     "time"
 7 )
 8 
 9 func main() {
10     Map := make(map[int]int)
11     wg := &sync.WaitGroup{}
12 
13     for i := 0; i < 1000; i++ {
14         go func() {
15             wg.Add(1)
16             defer wg.Done()
17             for j := 0; j < 10000; j++ {
18                 Map[rand.Int()] = rand.Int()
19                 time.Sleep(time.Millisecond)
20             }   
21         }() 
22     }   
23     time.Sleep(time.Second * 5)
24     wg.Wait()
25     // 程序崩溃
26 }
View Code

 5 defer 调用函数时,函数在当前函数结束前调用,但传给调用函数的参数在defer所在行确定,而不在调用是确定。存在多个defer时,先出现后调用。

 1 package main
 2 
 3 import (
 4     "fmt"
 5 )
 6 
 7 type Data struct {
 8     data string
 9 }
10 
11 func Println(data Data) {
12     fmt.Println(data.data)
13 }
14 func main() {
15     data := Data{"world "}
16     defer Println(data)
17     data.data = "hello "
18     defer Println(data)
19     // 输出: hello world 而不是: hello hello 或 world hello
20 }
View Code

 

 

   

posted @ 2015-11-23 09:52  ~逍遥~  阅读(268)  评论(0编辑  收藏  举报