当前位置:网站首页>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
}
边栏推荐
- Go语言函数
- Go语言for循环
- Serval and rooted Tree (cf1153d) - DP
- Implement AOP and interface caching on WPF client
- Part II data link layer
- Final examination of theory and practice of socialism with Chinese characteristics 1
- The e-sports Internet cafe uses a 2.5G network card to experience the feeling of flying!
- RANSAC提取平面(MATLAB内置函数)
- ASCII code comparison table
- Serval and Rooted Tree(CF1153D)-DP
猜你喜欢

The scale of the global machine vision market continues to rise. Poe image acquisition card provides a high-speed transmission channel for industrial cameras

【数据可视化】使用 Apache Superset 可视化 ClickHouse 数据

从概率论基础出发推导卡尔曼滤波

【数据可视化】Apache Superset 1.2.0教程 (三)—— 图表功能详解

Cuckoo Hash

The input value "18-20000hz" is incorrect. The setting information is incomplete. Please select a company

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

重投农业,加码技术服务,拼多多底盘进一步夯实

Application scenario: wide application of Poe network card in NDI technology for live broadcast program production

The gateway starts other microservices first. When the gateway is started, the gateway cannot be started and there is no exception log; Start the gateway first, and all services can be started normall
随机推荐
Capriccio in the Internet Age
Mysql add 新增多个新字段并指定字段位置
Which Bluetooth headset is better within 500? Inventory of gifts for girls' Day
Product information | Poe network card family makes a collective appearance, the perfect partner of machine vision!
[file upload vulnerability 04] server side mime detection and bypass experiment (based on upload-labs-2 shooting range)
The gateway starts other microservices first. When the gateway is started, the gateway cannot be started and there is no exception log; Start the gateway first, and all services can be started normall
JMeter load test finds the maximum number of concurrent users (including step analysis)
[game theory complete information static game] strategic game
机器视觉工控机PoE图像采集卡应用解析
RANSAC extract cylinder (matlab built-in function)
New product release: domestic single port Gigabit network card is officially mass produced!
为什么需要微服务
Space transcriptome experiment | what factors will affect the quality of space transcriptome sequencing during the preparation of clinical tissue samples?
Part I physical layer
【指标体系】最新数仓指标体系建模方法
Object storage of CEPH distributed storage
Obsidian关系图谱如何让节点可以手动拖动
Solution to the problem of PHP strtotime obtaining natural monthly error
输入值“18-20000hz”错误,设置信息不完整,请选择单位
MySQL installation free configuration tutorial under Windows mysql-5.6.51-winx64 Zip version