当前位置:网站首页>Go language functions
Go language functions
2022-06-11 21:05:00 【Just number six Z】
Go The function of language
function
Define format
Functions form the logical structure of code execution . stay Go In language , The basic composition of a function is : keyword func、 Function name 、 parameter list 、 Return value 、 Function bodies and return statements .
Go The language function definition format is as follows :
func FuncName(/* parameter list */) (o1 type1, o2 type2/* Return type */) {
// The body of the function
return v1, v2 // Return multiple values
}
Function definition description :
- func: Function by keyword func Start statement
- FuncName: The name of the function , According to the contract , Function names start with lowercase letters private, Capitalized means public
- parameter list : Functions can have 0 Two or more parameters , The parameter format is : Variable name type , If multiple parameters are separated by commas , Default parameters... Are not supported
- Return type :
① The above return value declares two variable names o1 and o2( Name return parameters ), This is not necessary , You can only type without variable name
② If there is only one return value and the return value variable is not declared , Then you can omit , Parentheses including return values
③ If there is no return value , Then omit the last return information directly
④ If there is a return value , So you have to add... Inside the function return sentence
Custom function
No parameter no return value
func Test() {
// No parameter no return value function definition
fmt.Println("this is a test func")
}
func main() {
Test() // No parameter no return value function call
}
Return value with parameter or not
Common parameter list
func Test01(v1 int, v2 int) {
// The way 1
fmt.Printf("v1 = %d, v2 = %d\n", v1, v2)
}
func Test02(v1, v2 int) {
// The way 2, v1, v2 All are int type
fmt.Printf("v1 = %d, v2 = %d\n", v1, v2)
}
func main() {
Test01(10, 20) // Function call
Test02(11, 22) // Function call
}
Indefinite parameter list
- Indefinite parameter type
Indefinite parameter means that the number of parameters passed into a function is indefinite . To do that , First, you need to define the function to accept indefinite parameter types :
// Form like ...type The type of the format can only exist as the parameter type of the function , And it has to be the last parameter
func Test(args ...int) {
for _, n := range args {
// Traversal parameter list
fmt.Println(n)
}
}
func main() {
// Function call , Biography 0 To multiple parameters
Test()
Test(1)
Test(1, 2, 3, 4)
}
- The transfer of indefinite parameters
func MyFunc01(args ...int) {
fmt.Println("MyFunc01")
for _, n := range args {
// Traversal parameter list
fmt.Println(n)
}
}
func MyFunc02(args ...int) {
fmt.Println("MyFunc02")
for _, n := range args {
// Traversal parameter list
fmt.Println(n)
}
}
func Test(args ...int) {
MyFunc01(args...) // Deliver as is , Test() The parameters of are passed to MyFunc01
MyFunc02(args[1:]...) //Test() In the parameter list , The first 1 Parameters and later parameters are passed to MyFunc02
}
func main() {
Test(1, 2, 3) // Function call
}
No parameter return value
Function with return value , There must be a clear termination statement , Otherwise, it will cause compilation errors .
A return value
func Test01() int {
// The way 1
return 250
}
// The official advice : It's best to name the return value , Because the return value is unnamed ,
// Although it makes the code more concise , But it will cause poor readability of the generated documents
func Test02() (value int) {
// The way 2, Name the return value
value = 250
return value
}
func Test03() (value int) {
// The way 3, Name the return value
value = 250
return
}
func main() {
v1 := Test01() // Function call
v2 := Test02() // Function call
v3 := Test03() // Function call
fmt.Printf("v1 = %d, v2 = %d, v3 = %d\n", v1, v2, v3)
}
Multiple return values
func Test01() (int, string) {
// The way 1
return 250, "sb"
}
func Test02() (a int, str string) {
// The way 2, Name the return value
a = 250
str = "sb"
return
}
func main() {
v1, v2 := Test01() // Function call
_, v3 := Test02() // Function call , The first return value is discarded
v4, _ := Test02() // Function call , The second return value is discarded
fmt.Printf("v1 = %d, v2 = %s, v3 = %s, v4 = %d\n", v1, v2, v3, v4)
}
With parameter and return value
// seek 2 Minimum and maximum number
func MinAndMax(num1 int, num2 int) (min int, max int) {
if num1 > num2 {
// If num1 Greater than num2
min = num2
max = num1
} else {
max = num2
min = num1
}
return
}
func main() {
min, max := MinAndMax(33, 22)
fmt.Printf("min = %d, max = %d\n", min, max) //min = 22, max = 33
}
边栏推荐
- 应用业务层修改
- Mysql add 新增多个新字段并指定字段位置
- 第一部分 物理层
- Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
- How to manually drag nodes in the Obsidian relationship graph
- JVM运行时常量池以及直接内存
- 桌面以及文件资源管理器无限重启的解决
- Serval and Rooted Tree(CF1153D)-DP
- UDP、TCP
- 周刊02|不瞒你说,我其实是MIT的学生
猜你喜欢

新品发布:国产单电口千兆网卡正式量产!
![[data visualization] use Apache superset to visualize Clickhouse data](/img/4b/a73c2eb810f1d2b492e950afb2d0bc.png)
[data visualization] use Apache superset to visualize Clickhouse data

Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience

How to manually drag nodes in the Obsidian relationship graph

Unity package manager starting server stuck

Goland中在文件模板中为go文件添加个人声明

IDEA中,运行yarn命令,显示无法加载文件,因为在此系统上禁用运行脚本

How to Load Data from CSV (Data Preparation Part)

Windows icon display exception resolution. The desktop icon is abnormal, the start menu icon is abnormal, and the taskbar icon is abnormal. Icon cache location.

Weekly 02 | to tell you the truth, I am actually a student of MIT
随机推荐
RANSAC extraction plane (matlab built-in function)
Live broadcast with practice | 30 minutes to build WordPress website with Alibaba cloud container service and container network file system
Lr-link Lianrui makes its debut at the digital Expo with new products - helping the construction of new infrastructure data center
How to Load Data from CSV (Data Preparation Part)
go语言的goto语句
php pcntl_fork 创建多个子进程解析
银泰百货与淘宝天猫联合打造绿色潮玩展,助力“碳中和”
JVM运行时常量池以及直接内存
Solution to the problem of PHP strtotime obtaining natural monthly error
A man always becomes a man and a man always arrives (progress, harvest, growth, self-confidence)
gateway先启动其他微服务,在启动网关,网关启动不了,且无异常日志;先启动网关,所有服务能正常启动
Interviewer: what is the event flow and event model in JS?
Final examination of theory and practice of socialism with Chinese characteristics 1
桌面以及文件资源管理器无限重启的解决
Wechat applet Bluetooth development
Chinese text classification based on CNN
Serval and rooted Tree (cf1153d) - DP
Gestionnaire de paquets d'Unit é Starting Server Stuck
Cuckoo Hash
Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets