golang-101-hacks(8)——“nil slice” vs “nil map”

栏目: Go · 发布时间: 5年前

内容简介:在GO语言中Slice和map都是引用类型,默认值都是执行结果是:当一个切片的值为“nil”时,是可以对它进行添加数据等操作。

GO 语言中Slice和map都是引用类型,默认值都是 nil

package main

import "fmt"

func main() {
    var (
        s []int
        m map[int]bool
    )
    if s == nil {
        fmt.Println("The value of s is nil")
    }
    if m == nil {
        fmt.Println("The value of m is nil")
    }
}

执行结果是:

The value of s is nil
The value of m is nil

当一个切片的值为“nil”时,是可以对它进行添加数据等操作。

package main

import "fmt"

func main() {
    var s []int
    fmt.Println("Is s a nil? ", s == nil)
    s = append(s, 1)
    fmt.Println("Is s a nil? ", s == nil)
    fmt.Println(s)
}

执行结果是:

The result is like this:

Is s a nil?  true
Is s a nil?  false
[1]

需要注意的是当slice是nil和没有值两种状况的slice长度都是0

A caveat you should notice is the length of both nil and empty slice is 0 :

package main

import "fmt"

func main() {
    var s1 []int
    s2 := []int{}
    fmt.Println("Is s1 a nil? ", s1 == nil)
    fmt.Println("Length of s1 is: ", len(s1))
    fmt.Println("Is s2 a nil? ", s2 == nil)
    fmt.Println("Length of s2 is: ", len(s2))
}

运行结果如下

The result is like this:

Is s1 a nil?  true
Length of s1 is:  0
Is s2 a nil?  false
Length of s2 is:  0

将slice的值与' nil '进行比较,以检查它是否是' nil '。

对一个“nil”的map取值是没有问题的,但是存储“nil”的map会引起程序panic:

package main

import "fmt"

func main() {
    var m map[int]bool
    fmt.Println("Is m a nil? ", m == nil)
    fmt.Println("m[1] is ", m[1])
    m[1] = true
}

运行结果如下

Is m a nil?  true
m[1] is  false
panic: assignment to entry in nil map

goroutine 1 [running]:
panic(0x4cc0e0, 0xc082034210)
    C:/Go/src/runtime/panic.go:481 +0x3f4
main.main()
    C:/Work/gocode/src/Hello.go:9 +0x2ee
exit status 2

Process finished with exit code 1

因此对map进行数据操作是 需要提前进行初始化,如下所示

m := make(map[int]bool)

顺便补充一点,建议使用下面的代码方式来判断map中是否存在某个键值:

if v, ok := m[1]; !ok {
    .....
}

参考

The Go Programming Language .


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

腾讯传

腾讯传

吴晓波 / 浙江大学出版社 / 2017-1-1 / 58.00元

腾讯官方唯一授权的权威传记 著名财经作家吴晓波倾力之作 当市值最高的中国互联网公司,遇上中国财经界最冷静的一双眼睛 读懂腾讯,读懂中国互联网 . 内容简介 本书全景式地记录了腾讯崛起的经历,并以互联网的视角重新诠释了中国在融入全球化进程中的曲折与独特性。 从1998年开始创业到成为世界级互联网巨头,腾讯以即时通信工具起步,逐渐进入社交网络、互动娱乐、网络媒......一起来看看 《腾讯传》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试