Go语言的那些坑二

热烈欢迎你,相识是一种缘分,Echa 哥为了你的到来特意准备了一份惊喜,go学习资料《 》

目录:


Go语言的那些坑二


Golang中函数被看做是值,函数值不可以比较,也不可以作为map的key

请问以下代码能编译通过吗?

<code>import (
\t"fmt"
)

func main(){
\tarray := make(map[int]func ()int)

\tarray[func()int{ return 10}()] = func()int{
\t\treturn 12
\t}

\tfmt.Println(array)
}/<code>

答案:

<code>可以正常编译通过。/<code>

稍作改动,改为如下的情况,还能编译通过吗?


<code>import (
\t"fmt"
)

func main(){
\tarray := make(map[func ()int]int)

\tarray[func()int{return 12}] = 10

\tfmt.Println(array)

}/<code>


答案:

<code>不能编译通过。/<code>

在Go语言中,函数被看做是第一类值:(first-class values):函数和其他值一样,可以被赋值,可以传递给函数,可以从函数返回。也可以被当做是一种“函数类型”。例如:有函数func square(n int) int { return n * n },那么就可以赋值f := square,而且还可以fmt.Println(f(3))(将打印出“9”)。Go语言函数有两点很特别:

  • 函数值类型不能作为map的key
  • 函数值之间不可以比较,函数值只可以和nil作比较,函数类型的零值是nil

匿名函数作用域陷阱

请看下列代码输出什么?


<code>import (
\t"fmt"
)

func main(){
\tvar msgs []func()
\tarray := []string{

\t\t"1", "2", "3", "4",
\t}

\tfor _, e := range array{

\t\t\tmsgs = append(msgs, func(){
\t\t\tfmt.Println(e)
\t\t})
\t}

\tfor _, v := range msgs{
\t\tv()
\t}
}/<code>


答案:

<code>4
4
4
4/<code>

在上述代码中,匿名函数中记录的是循环变量的内存地址,而不是循环变量某一时刻的值。

想要输出1、2、3、4需要改为:

<code>import (
\t"fmt"
)

func main(){
\tvar msgs []func()
\tarray := []string{
\t\t"1", "2", "3", "4",
\t}

\tfor _, e := range array{
\t\telem := e
\t\tmsgs = append(msgs, func(){
\t\t\tfmt.Println(elem)
\t\t})
\t}


\tfor _, v := range msgs{
\t\tv()
\t}
}/<code>

其实就加了条elem := e看似多余,其实不,这样一来,每次循环后每个匿名函数中保存的就都是当时局部变量elem的值,这样的局部变量定义了4个,每次循环生成一个。

[3]int 和 [4]int 不算同一个类型

请看一下代码,请问输出true还是false

<code>import (
"fmt"
"reflect"
)

func main(){
arrayA := [...]int{1, 2, 3}
arrayB := [...]int{1, 2, 3, 4}
fmt.Println(reflect.TypeOf(arrayA) == reflect.TypeOf(arrayB))
}/<code>

答案是:

<code>false/<code>

数组长度是数组类型的一个组成部分,因此[3]int和[4]int是两种不同的数组类型。

数组还可以指定一个索引和对应值的方式来初始化。

例如:


<code>func main() {
var sampleMap map[string]int
sampleMap = map[string]int {
"test1":1,
}
sampleMap["test"] = 1
fmt.Println(sampleMap)
}/<code>

会输出:


<code>map[test1:1 test:1]/<code>

有点像PHP数组的感觉,但是又不一样:arrayA的长度是多少呢?


<code>map[test1:1 test:1]/<code>

答案是:


<code>map[test1:1 test:1]/<code>

没错,定义了一个数组长度为4的数组,指定索引的数组长度和最后一个索引的数值相关,例如:r := [...]int{99:-1}就定义了一个含有100个元素的数组r,最后一个元素输出化为-1,其他的元素都是用0初始化。

不能对map中的某个元素进行取地址&操作


<code>map[test1:1 test:1]/<code>

map中的元素不是一个变量,不能对map的元素进行取地址操作,禁止对map进行取地址操作的原因可能是map随着元素的增加map可能会重新分配内存空间,这样会导致原来的地址无效

当map为nil的时候,不能添加值


<code>map[test1:1 test:1]/<code>

输出报错:


<code>map[test1:1 test:1]/<code>

必须使用make或者将map初始化之后,才可以添加元素。

以上代码可以改为:


<code>map[test1:1 test:1]/<code>

可以正确输出:

<code>map[test1:1 test:1]/<code>

&dilbert.Position和(&dilbert).Position是不同的

&dilbert.Position相当于&(dilbert.Position)而非(&dilbert).Position

请看例子:

请问输出什么?

<code>func main(){
type Employee struct {
ID int
Name string
Address string
DoB time.Time
Position string
Salary int
ManagerID int
}
var dilbert Employee

dilbert.Position = "123"

position := &dilbert.Position
fmt.Println(position)

}/<code>

输出:

<code>0xc42006c220/<code>

输出的是内存地址

修改一下,把&dilbert.Position改为(&dilbert).Position

<code>func main(){
type Employee struct {
ID int
Name string
Address string
DoB time.Time
Position string
Salary int
ManagerID int
}
var dilbert Employee

dilbert.Position = "123"

position := &dilbert.Position
fmt.Println(position)

}/<code>

输出:

<code>123/<code>

Go语言中函数返回的是值的时候,不能赋值

请看下面例子:

<code>type Employee struct {
ID int
Name string
Address string
DoB time.Time
Position string
Salary int
ManagerID int
}

func EmployeeByID(id int) Employee {
return Employee{ID:id}
}

func main(){
EmployeeByID(1).Salary = 0
}/<code>

请问能编译通过吗?

运行,输出报错:cannot assign to EmployeeByID(1).Salary

在本例子中,函数EmployeeById(id int)返回的是值类型的,它的取值EmployeeByID(1).Salary也是一个值类型;值类型是什么概念?值类型就是和赋值语句var a = 1或var a = hello world等号=右边的1、Hello world是一个概念,他是不能够被赋值的,只有变量能够被赋值。

修改程序如下:

<code>type Employee struct {
ID int
Name string

Address string
DoB time.Time
Position string
Salary int
ManagerID int
}

func EmployeeByID(id int) Employee {
return Employee{ID:id}
}

func main(){
var a = EmployeeByID(1)
a.Salary = 0
}/<code>

这就可以编译通过了

在声明方法时,如果一个类型名称本身就是一个指针的话,不允许出现在方法的接收器中

请看下面的例子,请问会编译通过吗?

<code>import (
\t"fmt"
)

type littleGirl struct{
\tName string
\tAge int
}

type girl *littleGirl

func(this girl) changeName(name string){
\tthis.Name = name
}

func main(){
\tlittleGirl := girl{Name:"Rose", Age:1}
\t
\tgirl.changeName("yoyo")
\tfmt.Println(littleGirl)
}/<code>

答案:

<code>不能编译通过,会提示“invalid receiver type girl(girl is a pointer type)”/<code>

Go语言中规定,只有类型(Type)和指向他们的指针(*Type)才是可能会出现在接收器声明里的两种接收器,为了避免歧义,明确规定,如果一个类型名本身就是一个指针的话,是不允许出现在接收器中的。

函数允许nil指针作为参数,也允许用nil作为方法的接收器

请看下面的例子,请问能编译通过吗?


<code>import (
\t"fmt"
)

type littleGirl struct{
\tName string
\tAge int
}


func(this littleGirl) changeName(name string){
\tfmt.Println(name)
}

func main(){
\tlittle := littleGirl{Name:"Rose", Age:1}

\tlittle = nil
\tlittle.changeName("yoyo")
\tfmt.Println(little)
}/<code>

答案:

<code>不能编译通过,显示"cannot use nil as type littleGirl in assignment"/<code>

Go语言中,允许方法用nil指针作为其接收器,也允许函数将nil指针作为参数。而上述代码中的littleGirl不是指针类型,改为*littleGirl,然后变量little赋值为&littleGirl{Name:"Rose", Age:1}就可以编译通过了。并且,nil对于对象来说是合法的零值的时候,比如map或者slice,也可以编译通过并正常运行。

Golang的时间格式化

不同于PHP的date("Y-m-d H:i:s", time()),Golang的格式化奇葩的很,不能使用诸如Y-m-d H:i:s的东西,而是使用2006-01-02 15:04:05这个时间的格式,请记住这个时间,据说这是Golang的诞生时间。


<code>time := time.Now()

time.Format("20060102") //相当于Ymd

time.Format("2006-01-02")//相当于Y-m-d

time.Format("2006-01-02 15:04:05")//相当于Y-m-d H:i:s

time.Format("2006-01-02 00:00:00")//相当于Y-m-d 00:00:00/<code>


分享到:


相關文章: