go语言快速入门:内建函数(6)
2017-02-03 06:29
861 查看
go语言中有一些比较常用的内建函数,在这篇文章中将会进行简单的介绍。
内建函数
函数名 | 说明 | 备注 |
---|---|---|
close | 关闭channel | 仅用于channel通讯 |
delete | 从map中删除实例 | map操作 |
len | 返回字符串,slice和数组的长度 | 可用于不同的类型 |
cap | 返回容量 | 可用于不同的类型 |
new | 内存分配 | 用于各种类型 |
make | 内存分配 | 仅用于chan/slice/map |
copy | 复制slice | slice操作 |
append | 追加slice | slice操作 |
panic | 报告运行时问题 | 异常处理机制 |
recover | 处理运行时问题 | 异常处理机制 |
内建打印函数 | 主要用于不引入fmt的时候的调试,实际使用时建议使用标准库fmt | |
println | 内建打印函数 | 主要用于不引入fmt的时候的调试,实际使用时建议使用标准库fmt |
complex | 构造复数类型 | 复数操作 |
real | 抽出复数的实部 | 复数操作 |
imag | 抽出复数的虚部 | 复数操作 |
len & cap
对象 | len返回值 | cap返回值 |
---|---|---|
数组 | 数组元素个数 | 数组元素个数 |
指向数组的指针 | 数组元素个数 | 数组元素个数 |
slice | 元素个数 | slice的最大size >=len(slice) |
map操作:delete
例子代码
[root@liumiaocn goprj]# cat basic-buildin1.go package main import "fmt" func main() { m1 := make(map[string]int) m1["Mon"] = 1 m1["Tue"] = 2 m1["Wed"] = 3 fmt.Println("before delete : m1=", m1, "len(m1)=", len(m1)) println("delete element by using key Tue") delete(m1, "Tue") fmt.Println("after delete : m1=", m1, "len(m1)=", len(m1)) } [root@liumiaocn goprj]#
执行结果
[root@liumiaocn goprj]# go run basic-buildin1.go before delete : m1= map[Mon:1 Tue:2 Wed:3] len(m1)= 3 delete element by using key Tue after delete : m1= map[Mon:1 Wed:3] len(m1)= 2 [root@liumiaocn goprj]#
slice操作
例子代码
[root@liumiaocn goprj]# cat basic-buildin2.go package main import "fmt" func main() { a1 := new([10]int) a1[4] = 5 a1[7] = 8 fmt.Println("a1= ", a1, "len(a1)=", len(a1), " cap(a1)=", cap(a1)) s1 := make([]int, 5, 10) s1[0] = 5 s1[4] = 2 s2 := make([]int, 5, 10) s2[0] = 1 s2[4] = 3 fmt.Println("before copy :s1= ", s1, "len(s1)=", len(s1), " cap(s1)=", cap(s1)) fmt.Println("before copy :s2= ", s2, "len(s2)=", len(s2), " cap(s2)=", cap(s2)) println("copy(s2,s1)") copy(s2, s1) fmt.Println("after copy :s1= ", s1, "len(s1)=", len(s1), " cap(s1)=", cap(s1)) fmt.Println("after copy :s2= ", s2, "len(s2)=", len(s2), " cap(s2)=", cap(s2)) println("reset") s2[0] = 1 s2[4] = 3 fmt.Println("after reset:s1= ", s1, "len(s1)=", len(s1), " cap(s1)=", cap(s1)) fmt.Println("after reset :s2= ", s2, "len(s2)=", len(s2), " cap(s2)=", cap(s2)) println("append(s2,20)") s2 = append(s2, 6) fmt.Println("after append:s1= ", s1, "len(s1)=", len(s1), " cap(s1)=", cap(s1)) fmt.Println("after append:s2= ", s2, "len(s2)=", len(s2), " cap(s2)=", cap(s2)) s2 = append(s2, 7) s2 = append(s2, 8) s2 = append(s2, 9) s2 = append(s2, 10) fmt.Println("after append:s2= ", s2, "len(s2)=", len(s2), " cap(s2)=", cap(s2)) s2 = append(s2, 11) fmt.Println("after append:s2= ", s2, "len(s2)=", len(s2), " cap(s2)=", cap(s2)) } [root@liumiaocn goprj]#
执行结果
[root@liumiaocn goprj]# go run basic-buildin2.go a1= &[0 0 0 0 5 0 0 8 0 0] len(a1)= 10 cap(a1)= 10 before copy :s1= [5 0 0 0 2] len(s1)= 5 cap(s1)= 10 before copy :s2= [1 0 0 0 3] len(s2)= 5 cap(s2)= 10 copy(s2,s1) after copy :s1= [5 0 0 0 2] len(s1)= 5 cap(s1)= 10 after copy :s2= [5 0 0 0 2] len(s2)= 5 cap(s2)= 10 reset after reset:s1= [5 0 0 0 2] len(s1)= 5 cap(s1)= 10 after reset :s2= [1 0 0 0 3] len(s2)= 5 cap(s2)= 10 append(s2,20) after append:s1= [5 0 0 0 2] len(s1)= 5 cap(s1)= 10 after append:s2= [1 0 0 0 3 6] len(s2)= 6 cap(s2)= 10 after append:s2= [1 0 0 0 3 6 7 8 9 10] len(s2)= 10 cap(s2)= 10 after append:s2= [1 0 0 0 3 6 7 8 9 10 11] len(s2)= 11 cap(s2)= 20 [root@liumiaocn goprj]#
复数操作
例子代码
[root@liumiaocn goprj]# cat basic-buildin3.go package main import "fmt" func main() { var c1 = complex(1.1, 2) var r1 = real(c1) var i1 = imag(c1) println("c1=", c1, " r1=", r1, " i1=", i1) fmt.Println("c1=", c1, " r1=", r1, " i1=", i1) } [root@liumiaocn goprj]#
执行结果
[root@liumiaocn goprj]# go run basic-buildin3.go c1= (+1.100000e+000+2.000000e+000i) r1= +1.100000e+000 i1= +2.000000e+000 c1= (1.1+2i) r1= 1.1 i1= 2 [root@liumiaocn goprj]#
相关文章推荐
- go语言快速入门:简介(1)
- go语言快速入门:流程控制(7)
- go语言快速入门:使用静态文件(20)
- go语言快速入门:JSON处理(24)
- go语言快速入门:第一个HelloWorld(2)
- go语言快速入门:模板应用(15)
- go语言快速入门:项目构建实践(21)
- go语言快速入门:自动化测试(17)
- go语言快速入门:IPC之Socket(9)
- go语言快速入门:数据类型使用例(4)
- go语言快速入门:模板应用(13)
- go语言快速入门:IPC之Socket(9)
- go语言快速入门:go的反射机制(22)
- Swift 1.1语言快速入门7.2使用无参函数
- go语言快速入门:数据类型使用例(5)
- go语言快速入门:Web应用的HelloWorld(11)
- go语言快速入门:template模板(12)
- Go语言快速入门
- go语言快速入门:简单工厂模式的实现(23)
- go语言快速入门:文档查询(19)