golang笔记之数据类型
月下独酌100 · · 1094 次点击 · · 开始浏览基础数据类型
整形、浮点数、复数、布尔型、常量
复合数据类型
slice、数组、map、struct
slices使用注意点:
- slice与数组的区别为在声明时不需要指定长度。
数组的初始化
var a [4]int
slice的初始化
var s []byte
Slices hold references to an underlying array, and if you assign one slice to another, both refer to the same array.
Slice持有一个潜在的数组,如果你将一个slice赋值给另一个slice,那么两个slice有共同的数组。重新分片一个slice不会拷贝此slice的内部数组。当只使用数据量比较大的sclice的一部分数据的时候,利用copy,这样方便源slice被回收。
var digitRegexp = regexp.MustCompile("[0-9]+")
func FindDigits(filename string) []byte {
b, _ := ioutil.ReadFile(filename)
return digitRegexp.Find(b)
}
To fix this problem one can copy the interesting data to a new slice before returning it:
func CopyDigits(filename string) []byte {
b, _ := ioutil.ReadFile(filename)
b = digitRegexp.Find(b)
c := make([]byte, len(b))
copy(c, b)
return c
}
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
基础数据类型
整形、浮点数、复数、布尔型、常量
复合数据类型
slice、数组、map、struct
slices使用注意点:
- slice与数组的区别为在声明时不需要指定长度。
数组的初始化
var a [4]int
slice的初始化
var s []byte
Slices hold references to an underlying array, and if you assign one slice to another, both refer to the same array.
Slice持有一个潜在的数组,如果你将一个slice赋值给另一个slice,那么两个slice有共同的数组。重新分片一个slice不会拷贝此slice的内部数组。当只使用数据量比较大的sclice的一部分数据的时候,利用copy,这样方便源slice被回收。
var digitRegexp = regexp.MustCompile("[0-9]+")
func FindDigits(filename string) []byte {
b, _ := ioutil.ReadFile(filename)
return digitRegexp.Find(b)
}
To fix this problem one can copy the interesting data to a new slice before returning it:
func CopyDigits(filename string) []byte {
b, _ := ioutil.ReadFile(filename)
b = digitRegexp.Find(b)
c := make([]byte, len(b))
copy(c, b)
return c
}