Go 基础 坑

1.字符串空为""

2. 传递的数组是原数组的拷贝,所以是无法通过传递数组的方法去修改原地址的数据的。在GO语言中除了切片(slice)、集合(map)、通道(channel)和接口(interface)之外,其它的都是值传递

3.在多行的Slice、Array和Map语句中遗漏逗号

package main

func main() {
x := []int{
1,
2 //error,需要加,
}
_ = x
}

Compile Errors:

syntax error: unexpected semicolon or newline, expecting comma or }

4.闭包中使用的变量若非作为参数传递,则都是引用传递

package main

import (
"fmt"
) func main() {
s := []string{"a","b","c"}
for _,v := range s {
go func () {
fmt.Println(v)
}()
}
select {}
}

输出

c
c
c
fatal error: all goroutines are asleep - deadlock!

5.append 方法在添加数据后超过原始长度会重新创建新的存储空间

package main

import (
"fmt"
) func Ping(s []int) {
s = append(s,3)
}
func main() {
s := make([]int, 0)
fmt.Println(s)
Ping(s)
fmt.Println(s)
}

  输出结果:

[]
[]

  最优写法:

package main

import (
"fmt"
) func Ping(s []int) []int {
s = append(s,3)
return s
}
func main() {
s := make([]int, 0)
fmt.Println(s)
s = Ping(s)
fmt.Println(s)
}

  6.使用time的Format时,使用自带常量

const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)

  

上一篇:WP8.1简单项目 《在线词典》


下一篇:cartographer 分析