Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What do functions, closures and recursion mean in Go

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the meaning of function, closure and recursion in Go language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "what is the meaning of function, closure and recursion in Go language".

GO function features: there is no need to declare a prototype. Variable parameters are supported. The return value type is written at the end, and multiple return values are supported. Support naming return parameters. Anonymous functions and closures are supported. A function is also a type, and a function can be assigned to a variable. Nested is not supported. A package cannot have two functions with the same name. Overloading (overload) is not supported. Default parameters (default parameter), optional parameters are not supported. Parameter passing: whether it is value passing or reference passing, what is passed to the function is a copy of the variable, but the value passing is a copy of the value. Reference delivery is a copy of the address.

Use the keyword func to define the function, and the opening curly braces still cannot be on another line.

The Go function declaration:

Package main

Import "fmt"

Func test (fn func () int) int {

Return fn ()

}

/ / define the function type.

Type FormatFunc func (s string, x, y int) string

Func format (fn FormatFunc, s string, x, y int) string {

Return fn (s, x, y)

}

Func main () {

S1: = test (func () int {return 100}) / / directly takes the anonymous function as a parameter.

S2: = format (func (s string, x, y int) string {

Return fmt.Sprintf (s, x, y)

}, "% d,% d", 10,20)

Println (S1, S2)

}

/ / return the result

/ / 100 10, 20

Parameters of GO function

Functions can pass parameters in two ways:

Value passing: means that a copy of the actual parameter is passed to the function when the function is called, so that if the parameter is modified in the function, the actual parameter will not be affected.

Reference passing: refers to passing the address of the actual parameter to the function when calling the function, so the modification of the parameter in the function will affect the actual parameter.

By default, the Go language uses value passing, which means that the actual parameters are not affected during the call.

Note 1: whether it is value passing or reference passing, what is passed to the function is a copy of the variable, but the value passing is a copy of the value. Reference delivery is a copy of the address, which is generally more efficient. The value copy depends on the size of the copied object, and the larger the object, the lower the performance.

Note that 2:map, slice, chan, pointer, and interface are passed by reference by default.

Any type of indefinite parameters: that is, the parameters of the function and the type of each parameter are not fixed.

Passing any type of data in interface {} is a common usage of the Go language, and interface {} is type safe.

Func myfunc (args... interface {}) {

}

When you use the slice object as a parameter, you must expand. (slice...)

Package main

Import (

"fmt"

)

Func test (s string, n... int) string {

Var x int

For _, I: = range n {

X + = I

}

Return fmt.Sprintf (s, x)

}

Func main () {

S: = [] int {1,2,3}

Res: = test ("sum:% d", s...) / / slice... Expand slice

Println (res)

}

The return value of GO function

"_" identifier, which is used to ignore a return value of a function

The return value of Go can be named and used like a variable declared at the beginning of the function body.

The name of the return value should have some meaning and can be used as a document.

Anonymous function

An anonymous function is a function implementation that does not need to define a function name.

In Go, functions can be passed or used like ordinary variables, and the Go language allows anonymous functions to be defined in code at any time.

An anonymous function consists of a function declaration and a function body without a function name. The advantage of anonymous functions is that you can use variables within the function directly without declaring it.

Package main

Import (

"fmt"

"math"

)

Func main () {

GetSqrt: = func (a float64) float64 {

Return math.Sqrt (a)

}

Fmt.Println (getSqrt (4))

}

/ / return

/ / 2

The above first defines a variable named getSqrt, which is somewhat different from the previous variable initialization. Func,func is used to define the function, but the biggest difference between this function and the function mentioned above is that there is no function name, that is, anonymous function. Here a function is treated as a variable.

Golang anonymous functions can be assigned to variables, as structure fields, or passed in channel.

Package main

Func main () {

/ /-function variable--

Fn: = func () {println ("Hello, World!")}

Fn ()

/ /-function collection--

Fns: = [] (func (x int) int) {

Func (x int) int {return x + 1}

Func (x int) int {return x + 2}

}

Println (fns [0] (100))

/ /-function as field--

D: = struct {

Fn func () string

} {

Fn: func () string {return "Hello, World!"}

}

Println (d.fn ())

/ /-channel of function--

Fc: = make (chan func () string, 2)

Fc

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report