当前位置:网站首页>Go language functions as parameters of functions
Go language functions as parameters of functions
2022-06-12 13:44:00 【You stole my name】
Go Language functions are used as arguments to functions
- go Expressions for language functions
func Function name ( The name of the parameter 1 type 1, The name of the parameter 2 type 2,…)( Return type 1, Return type 2,…) {
The body of the function
}
Be careful :go Language functions support multiple return values . Capitalized function name means that the function is Public Can be used by other package call , Lowercase private, Cannot be called by other packages .
func myFunc(number1 int32,number2 int32) (int32,int32) {
return number1+number2 ,number1 -number2
}
- go Functions in a language have their own types .
package main
import (
"fmt"
)
func addNumber(a1 int,a2 int) int {
return a1+a2
}
func main() {
fmt.Printf("addNumber The type is %T:",addNumber)
}
The execution result of the above code is :
- go Languages can treat functions as data types , For example, declare a addNumber Variables of type are as follows :
// Method 1
a1:=addNumber
// Method 2
// Statement a1 The type of func(int,int) int, And give the initial value addNumber
// Here is an analogy var a2 int=32,addNumber amount to 32 Is a constant , Given func(int,int) int Type specific values .
var a1 func(int,int) int=addNumber
- Function types are used as formal parameters of functions
Such as , You can define one of the following functions :
package main
import (
"fmt"
)
func addNumber(a1 int,a2 int) int {
return a1+a2
}
func addNumber2(a3 func(int,int) int,a4 int) int {
return a3(a4,a4)+a4
}
func main() {
//fmt.Printf("addNumber The type is %T:",addNumber)
fmt.Printf("addNumber2 The type is :%T\n",addNumber2)
fmt.Printf("addNumber2 The result of the implementation of :%v\n",addNumber2(addNumber,34))
}
The execution result is :
Here we can simplify by using alias :
package main
import (
"fmt"
)
func addNumber(a1 int,a2 int) int {
return a1+a2
}
type myFunc func(int,int) int
func addNumber2(a3 myFunc,a4 int) int {
return a3(a4,a4)+a4
}
func main() {
//fmt.Printf("addNumber The type is %T:",addNumber)
fmt.Printf("addNumber2 The type is :%T\n",addNumber2)
fmt.Printf("addNumber2 The result of the implementation of :%v\n",addNumber2(addNumber,34))
}
Notice that the function type has changed at this time , The execution result is :
边栏推荐
- Successfully rated Tencent t3-2, 10000 word parsing
- Codeforces 1637 E. best pair - Thinking
- AVFoundation
- Transmission and response of events and use cases
- Seekg, tellg related file operations
- Wechat web developer tools tutorial, web development issues
- jsp跳转问题,不能显示数据库数据,并且不能跳转
- Introduction to color coding format
- Code debugging - print log output to file
- Deploy opengauss database based on Huawei cloud Kunpeng elastic ECS [Gauss is not a mathematician this time]
猜你喜欢

聊聊MySQL的10大经典错误

Tensorrt, onnx to tensorrt in mmclas

Relevant knowledge points of cocoapods

leetcode 47. Permutations II full permutations II (medium)
![[wustctf2020] selfie score query -1](/img/dc/47626011333a0e853be87e492d8528.png)
[wustctf2020] selfie score query -1

Paw advanced user guide

618 entered the second half of the period, apple occupied the high-end market, and the domestic mobile phones finally undercut the price competition

import torch_ Data view of geometric

Application of binary search -- finding the square root sqrt of a number

torch_ geometric message passing network
随机推荐
Transmission and response of events and use cases
Web3.0, the era of "stimulating creativity"
MySQL 查询 limit 1000,10 和 limit 10 速度一样快吗? 深度分页如何破解
Teach you how to create SSM project structure in idea
【刷题篇】超级洗衣机
Title: Yanghui triangle
Innovation training (x) advanced interface beautification
Resume NFT platform trustrecruit joined Octopus network as a candidate application chain
Codeforces 1629 C. Mexico array - simple greed
基于华为云鲲鹏弹性云服务器ECS部署openGauss数据库【这次高斯不是数学家】
Successfully rated Tencent t3-2, 10000 word parsing
Pytorch to onnx, onnxruntime reasoning in mmclas
开发中使用的语言技巧
There was an error installing mysql. Follow the link below to CMD
Xcode debugging OpenGLES
jsp跳转问题,不能显示数据库数据,并且不能跳转
Realization of Joseph Ring with one-way ring linked list
颜色编码格式介绍
Time processing in C language (conversion between string and timestamp)
字节序数据读写