Golang:make,slice和append的注意细节

作者: 不屈真实 | 来源:发表于2018-11-19 19:04 被阅读147次

使用append向slice追加元素遇到的坑,在此总结一下。
一,细节1

urls := make(map[string]string, 3) 
urls["baidu"] = "www.baidu.com"
urls["google"] = "www.google.com"
urls["csdn"] = "www.csdn.net"
names := make([]string, len(urls))
for key, _ := range urls {
    names = append(names, key)
    }
fmt.Println(names,len(names))

输出结果
[ csdn baidu google] 6
前面多了几个空格,长度为6,与预期的结果不一致
修改代码

names := make([]string, 0)
for key, _ := range urls {
    names = append(names, key)
}
fmt.Println(names,len(names))

或者

var names []string
for key, _ := range urls {
    names = append(names, key)
}
fmt.Println(names,len(names))

输出结果
[baidu google csdn] 3
总结:append无论如何都是从slice的尾部开始追加数据,原来的slice只有3个长度,现在每一次append都要重新分配一次内存
二、细节2

s1 := []int{1, 2, 3}
s2 := []int{4, 5}
s1 = append(s1, s2)
fmt.Println(s1)

程序报错:cannot use s2 (type []int) as type int in append
修改为:

s1 := []int{1, 2, 3}
s2 := []int{4, 5}
s1 = append(s1, s2...)
fmt.Println(s1)

输出结果:
[1 2 3 4 5]
总结:append切片时候别漏了'...'
三、细节3
new与make区别:
new只分配内存它并不初始化内存,只是将其置零。new(T)会为T类型的新项目,分配被置零的存储,并且返回它的地址,一个类型为T的值,也即其返回一个指向新分配的类型为T的指针,这个指针指向的内容的值为零(zero value),注意并不是指针为零。比如,对于bool类型,零值为false;int的零值为0;string的零值是空字符串。
make用于slice,map,和channel的初始化,返回一个初始化的(而不是置零),类型为T的值(而不是
T)。之所以有所不同,是因为这三个类型是使用前必须初始化的数据结构。例如,slice是一个三元描述符,包含一个指向数据(在数组中)的指针,长度,以及容量,在这些项被初始化之前,slice都是nil的。对于slice,map和channel,make初始化这些内部数据结构,并准备好可用的值。

p := new([]int) //p == nil; with len and cap 0,被置零的slice结构体的指针,即指向值为nil的slice的指针
fmt.Println(p)

v := make([]int, 10, 50) // v is initialed with len 10, cap 50
fmt.Println(v)

输出结果:
&[]
[0 0 0 0 0 0 0 0 0 0]

new1 := new([2]int)
fmt.Println(new1)
new1[0] = 1
new1[1] = 2
fmt.Println(new1)

输出结果:
&[0 0]
&[1 2]

相关文章

  • Golang:make,slice和append的注意细节

    使用append向slice追加元素遇到的坑,在此总结一下。一,细节1 输出结果[ csdn baidu g...

  • golang 删除 slice 中的部分元素

    golang 对 slice 的操作全在 [:] 中 和 append Noticesslice[A:B], 包涵...

  • slice扩容

    原文:【Golang】slice类型存什么?make和new?slice和数组?扩容规则?_哔哩哔哩_bilibi...

  • 【Golang】make和new区别,append

    根据GO夜读学习go源码 源码: Demo Output Append Demo 在往slice里append元素...

  • Golang的append工作流程

    总感觉golang的append设计反人类,不是在一个slice中追加元素,而是生成一个新的slice。正好今天被...

  • golang append

    append属于内置函数,用于 slice 的元素添加操作。 slice在操作时需要注意:slice底层指向一个数...

  • Golang中Slice的append详解

    Go中可以使用“+”合并字符串,但是这种合并方式效率非常低,每合并一次,都是创建一个新的字符串,就必须遍历复制一次...

  • golang-101-hacks(9)——追加元素

    注:本文是对golang-101-hacks中文翻译。在Slice中添加元素:可以使用“Go”内置函数append...

  • golang Tips

    最后更新时间: 2017/06/12 增加5. golang 列出依赖的包 1. slice make时候如果le...

  • golang代码级调优

    golang代码级调优方法 基本优化 提前预估size, make之临时的map,slice,可采用sync.po...

网友评论

    本文标题:Golang:make,slice和append的注意细节

    本文链接:https://www.haomeiwen.com/subject/auwafqtx.html