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

How to use type func () of Go language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how to use type func () of Go language". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use type func () of Go language".

1. Pre-foundation 1. Go function basis

In the GE language, the basic components of functions are: keyword func, function name, parameter list, return value, function body and return statement.

Func function name (argument list) returns the value type {inside the function body}

The Go language is a language with strong data types, and parameters must be typed or an error will be reported. Func is the declaration keyword of the function.

1) return value

The definition of the return value can give the return value a name, and unlike other programming languages, there can be multiple return values of functions in the go language.

Func addTwoNumber (an int, b int) (int, int) {return aquib, a murb}

At the same time, you can give the return value an alias.

Func addTwoNumber (an int, b int) (sum, dif int) {return aquib, a murb}

After naming the return value like this, you can directly assign a value to the return value

Func addTwoNumber (an int, b int) (sum, dif int) {sum = a+bdif = a-breturn}

Similarly, you can directly return the values of sum and dif.

2) variable parameters of the function

Func sum (numbers... int) int {s: = 0for I: = range numbers {s + = I} return s} func main () {fmt.Println (sum)

Like this, we can pass multiple values to the sum function.

two。 Function signature

The function type is also called the function signature. The type of a function is the function name, parameter name and {removed from the first line of the function definition. You can print the type of the function using the "% T" format parameter of fmt.Printf.

The function type is the same

The condition that the two function types are the same is that they have the same formal parameter list and return value list (the order, number and type of list elements are the same), and the formal parameter names can be different. * * the following three functions have exactly the same function type.

Func add (a, b int) int {return a + b} func sub (c int, d int) int {return c-d} func mul (e int, f int) int {return e * f}

Verify demo

Package mainimport "fmt" func add (a, b int) int {return a + b} func sub (c int, d int) int {return c-d} func mul (e int, f int) int {return e * f} func main () {fmt.Printf ("% T\ n", add) fmt.Printf ("% T\ n", sub) fmt.Printf ("% T\ n", mul)}

The output result of executing go run test.go is as follows:

Func (int, int) int

Func (int, int) int

Func (int, int) int

II. Type func () usage of Go language | type func () custom function type 1. Golang defines function types through type

In the Go language, type can define any custom type

For example, familiar ones: type dog struct {}, type myInt int and so on.

So func can also be customized as a type, type myFunc func (int) int, which means a custom function type called myFunc. The signature of this function must conform to the input as int and the output as int.

Golang defines function types through type

Function types can be defined through type in the following format

Type typeName func (arguments) retType

The function type is also a type, so it can be defined as a function input parameter. In the go language, the function name can be regarded as a constant of the function type, so we can directly pass the function name as a parameter to the function.

Verify demo

Package mainimport "fmt" func add (a, b int) int {return a + b} / / sub as a function name can be regarded as a constant of type op func sub (a, b int) int {return a-bhand / define function type optype opfunc (a, b int) int// formal parameter specifies the incoming parameter as function type opfunc Oper (fu op, a, b int) int {return fu (a) B) func main () {/ / the function name can be regarded as a constant of the function type in the go language So we can pass the function name as an argument directly into the function. Aa: = Oper (add, 1,2) fmt.Println (aa) bb: = Oper (sub, 1,2) fmt.Println (bb) above is all the content of this article entitled "how to use type func () in Go language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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