教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 go语言快速入门:内建函数(6)

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

发布时间:2021-04-16   编辑:jiaochengji.com
教程集为您提供go语言快速入门:内建函数(6)等资源,欢迎您收藏本站,我们将为您提供最新的go语言快速入门:内建函数(6)资源

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语言快速入门:内建函数(6)”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
Golang 基础教程
想系统学习GO语言(Golang
从零学习 Go 语言(24):理解 Go 语言中的 goroutine
Go语言的主要特性和发展影响
Go语言发展历史、核心、特性及学习路线
从零开始学习GO语言-搭建Go语言开发环境-快速开发入门第一个小程序
Go 语言到底适合干什么?
为何要用go语言
Golang库集合
go run main.go 参数_Go语言入门:Hello world

[关闭]
~ ~