您的位置:首页 > 编程语言 > Go语言

go语言快速入门:内建函数(6)

2017-02-03 06:29 861 查看
go语言中有一些比较常用的内建函数,在这篇文章中将会进行简单的介绍。

内建函数

函数名说明备注
close关闭channel仅用于channel通讯
delete从map中删除实例map操作
len返回字符串,slice和数组的长度可用于不同的类型
cap返回容量可用于不同的类型
new内存分配用于各种类型
make内存分配仅用于chan/slice/map
copy复制sliceslice操作
append追加sliceslice操作
panic报告运行时问题异常处理机制
recover处理运行时问题异常处理机制
print内建打印函数主要用于不引入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语言 函数