1024programmer Java golang(06) function introduction

golang(06) function introduction

Original link http://www.limerence2017.com/2019/09/11/golang11/#more

Function Introduction

Function is an indispensable part of the programming language. In the language Golang, functions are first-class citizens. It is also an essential skill to use golang well.
Look at the format of golang functions

1
2
3
func function name(function parameters) return value type{

}

A simple function

1
2
3
func HelloFunc(str string) string{
return str
}

This function returns the passed in string, the function call is as follows

1
fmt.Println(Hello("Nice to meet you!"))

Return multiple return values

golang allows functions to return multiple return values

1
2
3
4
5
6
7
8
func Add(a int, b int) (ret int, err error) {
if a <0 || b <0 {
//Negative numbers are not allowed to be added
err = errors.New("should be non-negative numbers")
return
}
return a + b, nil
}

The function returns the result of adding two numbers and an error. Call this function below

1
2
3
4
5
6
res, err:=Add(100, 200) 
if err != nil{
fmt.Println("add error ! ")
return
}
fmt. Println("add result is ", res)

Label can be used inside the function

1
2
3
4
5
6
7
8
9
func myfunc() {
i := 0
HERE:
fmt. Println(i)
i++
if i <10 {
goto HERE
}
}

goto will jump to fmt.Println(n) to continue execution, achieving the effect of i loop self-increment and output

Function variable parameters

Golang allows functions to use variable parameters

1
2
3
4
5
6
//Indefinite parameters
func myfuncv(args ...int) {
for _, arg := range args {
fmt.Println(arg)
}
}

The way to write variable parameters is to add… type after the parameter name. Let’s call this function

1
myfuncv(1,2,4,7,8)

Variable parameters can obtain elements through slicing. Write another function

1
2
3
4
5
6
func myfuncv3(args ...  int) {
//Pass it as is
myfuncv( args...)
//Passed by slice
myfuncv(args[1:]...)
}

This function internally passes the parameters to myfuncv using expansion. args is a slice and is expanded with…, so that myfuncv can continue processing.
The following is a more complicated function with interface parameters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func MyPrintf(args ...  interface{}) {
for _, arg := range args {
//interface Any type,
//arg. (type) can only be used in switch structures
switch arg.(type) {
case int:
fmt.Println(arg, "is an int value.")
case string:
fmt.Println(arg, "is a string value.")
case int64:
fmt.Println(arg, "is an int64 value")
default:
fmt.Println(arg, "is an unknown type")
}
}
}

The MyPrintf function traverses the parameter args and determines the type according to the type, and makes corresponding output. Adding .(type) after a variable of interface type can return its type. The relevant knowledge of the interface will be explained later.
That’s it for now, my official account
Technical Pictures

golang(06) function introduction



�This is my official account
Technical picture

Golang(06) function introduction


This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/724853

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索