南君手记
  • 持续专注
  • Docker
    • Azure容器镜像代理服务失效
    • registry镜像加速器拉取谷歌镜像
    • 简单镜像加速
    • registry拉取dockerhub私有镜像
  • Golang
    • standard_init_linux.go:190: exec user process caused "no such file or directory
    • flota64后面保留三位小数
    • 数组赋值问题
    • append函数的使用方式
    • 错误返回非空的值
    • 数组类型转换
    • 强制类型转换
    • for循环之迭代变量
    • 切片长度
    • Map面试题
    • 指针方法&值方法
  • Gitbook
  • 油管知识汇
  • 穷查理宝典
    • 一些箴言
  • 自省
  • 技术调研
    • gomonkey
    • goconvey调研学习
    • Github认证调研
  • kubenetes
    • 利用NFS动态提供Kubernetes后端存储卷
    • k8s CRD(一)quick start
  • Tekton
    • Tekton Dashboard
    • tekton之Task&TaskRun概念篇
    • tekton简单介绍及安装
  • 哈佛的6堂独立思考课
由 GitBook 提供支持
在本页

这有帮助吗?

  1. Golang

append函数的使用方式

内置函数append使用方式

看一下内置函数append在buildin.go中的注释就知道了

// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
//  slice = append(slice, elem1, elem2)
//  slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
//  slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type

注释翻译如下:

apeend内中函数是将元素添加到slice的末尾,如果容量足够,目标元素被容纳进切片。如果容量不足,一个新的底层数组将被分配,append函数返回结果是一个更新后的切片。因此,有必要将添加的结果存储在通常包含切片本身的变量中。

append的使用方式:

slice = append(slice, elem)

slice = append(slice, elem1, elem2)

slice = append(slice, anotherSlice...) // 最后这三个点点点要加上,不然会报错,这是用于两个切片的合并的。

当然还有例外:

将string拼接到byte切片是合法的,例如

slice = append([]byte("hello "), "world"...)

上一页数组赋值问题下一页错误返回非空的值

最后更新于5年前

这有帮助吗?