2.3 Go built-in functions

Built-in functions Go language has some built-in functions do not require an import operation can be used. Sometimes they may operate for different types, for example: len, cap and append, or to be used for system level operations, such as: panic. Therefore, they need direct support of the compiler.

append          -- 用来追加元素到数组、slice中,返回修改后的数组、slice
close           -- 主要用来关闭channel
delete            -- 从map中删除key对应的value
panic            -- 停止常规的goroutine  (panic和recover:用来做错误处理)
recover         -- 允许程序定义goroutine的panic动作
imag            -- 返回complex的实部   (complex、real imag:用于创建和操作复数)
real            -- 返回complex的虚部
make            -- 用来分配内存,返回Type本身(只能应用于slice, map, channel)
new                -- 用来分配内存,主要用来分配值类型,比如int、struct。返回指向Type的指针
cap                -- capacity是容量的意思,用于返回某个类型的最大容量(只能用于切片和 map)
copy            -- 用于复制和连接slice,返回复制的数目
len                -- 来求长度,比如string、array、slice、map、channel ,返回长度
print、println     -- 底层打印函数,在部署环境中建议使用 fmt 包

init function

go language initfunctions for packet (package)initialization, the function is an important feature of the language go.

It has the following characteristics:

1 init函数是用于程序执行前做包的初始化的函数,比如初始化包里的变量等

2 每个包可以拥有多个init函数

3 包的每个源文件也可以拥有多个init函数

4 同一个包中多个init函数的执行顺序go语言没有明确的定义(说明)

5 不同包的init函数按照包导入的依赖关系决定该初始化函数的执行顺序

6 init函数不能被其他函数调用,而是在main函数执行之前,自动被调用

mainfunction

Go语言程序的默认入口函数(主函数):func main()
函数体用{}一对括号包裹。

func main(){
    //函数体
}

initFunction and the mainsimilarities and differences between functions

相同点:
    两个函数在定义时不能有任何的参数和返回值,且Go程序自动调用。
不同点:
    init可以应用于任意包中,且可以重复定义多个。
    main函数只能用于main包中,且只能定义一个。

The order of execution of two functions: to go with a document init()calling sequence is from top to bottom. For the same packagein different files by file name string comparison is "small to large" order of calls each file init()functions. For different package, if not dependent, then, calls its package in accordance with the main package "first call after import" sequence init(), if packagethere is a dependency, then the first call first to be dependent on packagethe init()last calling the main function.

Guess you like

Origin www.cnblogs.com/open-yang/p/11256712.html