博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go的nil map有什么卵用?
阅读量:4141 次
发布时间:2019-05-25

本文共 1740 字,大约阅读时间需要 5 分钟。

     在C++ STL中,只有empty map, 没有NULL map,  但是, 在go中,既有empty map, 又有nil map.  为什么要怎么搞? 在我看来, 完全没有必要搞个nil map,  所有的map应该都是直接自动初始化为empty map.  既然go设计者这个搞了, 自然有他们的理由, 我不认同, 但没有什么卵用。

      来看看go圣经怎么说:The zero value for a map type is nil, that is, a reference to no hash table at all.  这个nil map是不能添加元素的, 必须进行初始化才可以。 既然如此, 何不默认初始化为empty map呢? 不解!

      对C++的NULL进行操作,总是让人提醒吊胆, 那么在nil map上操作, 会怎样呢? 继续看go圣经: Most operations on maps, including lookup, delete, len, and range loops, are safe to perform on a nil map reference, since it behaves like an emtpy map. But storing to a nil map cause a panic.

      最后来看:

package main import (    "fmt") func main(){    var a []int        if a == nil {        fmt.Println("it is nil, 1")    }else{        fmt.Println("it not not nil, 1")    }        fmt.Println("slice len1 is", len(a))        a = make([]int, 0)  // 不必要        if a == nil {        fmt.Println("it is nil, 2")    }else{        fmt.Println("it not not nil, 2")    }        fmt.Println("slice len2 is", len(a))           a = append(a, 1)                        var m map[int]int        if m == nil {        fmt.Println("it is nil, 3")    }else{        fmt.Println("it not not nil, 3")    }        fmt.Println("map len3 is", len(m))        m = make(map[int]int, 0)  // 必要,否则panic        if m == nil {        fmt.Println("it is nil, 4")    }else{        fmt.Println("it not not nil, 4")    }        fmt.Println("map len4 is", len(m))        m[1] = 10        fmt.Println(a, m)}

       结果:

it is nil, 1

slice len1 is 0
it not not nil, 2
slice len2 is 0
it is nil, 3
map len3 is 0
it not not nil, 4
map len4 is 0
[1] map[1:10]
 

     由此可见:

     对于nil slice而言, 爱咋地咋地, append和len都可以.

     对于nil map而言, 不能直接赋值, 但仍可以用len.   真是个鸡肋。

 

     对于empty slice而言, 爱咋地咋地, append和len都可以.       

     对于empty map而言, 爱咋地咋地, append和len都可以.            

     

     

 

 

     

转载地址:http://tczti.baihongyu.com/

你可能感兴趣的文章
九度:题目1008:最短路径问题
查看>>
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
51nod 分类
查看>>
1136 . 欧拉函数
查看>>
面试题:强制类型转换
查看>>
Decorator模式
查看>>
Template模式
查看>>
Observer模式
查看>>
高性能服务器设计
查看>>
性能扩展问题要趁早
查看>>
MySQL-数据库、数据表结构操作(SQL)
查看>>
OpenLDAP for Windows 安装手册(2.4.26版)
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>