[Go] Slices vs Array
2024-10-18 21:29:06
It is recommended to use 'slice' over 'Array'.
An array variable denotes the entire array; it is not a pointer to the first array element (as would be the case in C). This means that when you assign or pass around an array value you will make a copy of its contents.
Arrays have their place, but they're a bit inflexible, so you don't see them too often in Go code. Slices, though, are everywhere. They build on arrays to provide great power and convenience.
letters := []string{"a", "b", "c", "d"}
You can call built-in function:
var s []byte
s = make([]byte, , )
// s == []byte{0, 0, 0, 0, 0}
When you modifiy the slices, it pass around the reference:
d := []byte{'r', 'o', 'a', 'd'}
e := d[2:]
// e == []byte{'a', 'd'}
e[1] = 'm'
// e == []byte{'a', 'm'}
// d == []byte{'r', 'o', 'a', 'm'}
More information: https://blog.golang.org/go-slices-usage-and-internals
最新文章
- for循环相关
- 【卡西欧Fx5800-p程序】01 坐标转换程序-带注释
- spark能否取代Hadoop?
- 你需要管理员权限才能删除文件夹及服务器C盘不及批处理
- 用ubuntu下载电影:磁力链接,torrent,迅雷链接
- 5分钟实现Android中更换头像功能
- 把Nginx加为系统服务(service nginx start/stop/restart)
- The account is locked
- layer 模版使用
- 使用Linux静态库
- C#实战Microsoft Messaging Queue(MSMQ)
- strpos、 strstr、 substr三个函数的对比讲解
- ARTS打卡第三周
- (转)urllib库python2和python3具体区别
- 位运算------按位与、按位或、按位异或、取反、<;<;、>;>;、>;>;>;
- Python爬虫之requests+正则表达式抓取猫眼电影top100以及瓜子二手网二手车信息(四)
- String中对字符串进行操作的一些方法
- HTML 练习 做简历表
- HTML5新增的form属性简介——张鑫旭
- 【mlflow】打包:npm run build + python setup.py sdist