当前位置:网站首页>Variable parameters of go
Variable parameters of go
2022-06-30 06:01:00 【Flower master】
Variable function
Conditions :
1、 The last parameter uses ...
2、 If you put variable parameters before parameters, all parameters will be regarded as variable parameters , Compilation will also report errors syntax error: cannot use … with non-final parameter
3、 Variable parameters need to use range To cycle
4、 The variable parameter is actually a slice [] slice
Operation principle :
1、 First, variable parameters will be converted to corresponding types slice
2、 Pass this slice to the function
Variable parameters vs section
1、 Improve the readability
2、 Each time you call a function, you need to create a slice , It's not necessary
append Is a variable function
To pass a slice to a variable parameter, you need to set the slice []string{}... Add 3 One point is enough , And there will be no new slices
Code
package main
import "fmt"
/**
* Variable parameters
*/
func main() {
test1(1,2,3,4)
test2(1,2,3,4)
t := []int{2,3,4}
test2(1,t...) // Slice and add ... Can still pass
}
func test1(a ...int) {
fmt.Println(a)
}
func test2(a int , b ...int) {
fmt.Println(a,b)
}
// This definition is , It will report an error
func test3(b ...int, a int) {
}
边栏推荐
- MySQL 索引
- Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
- 云服务器部署 Web 项目
- What do you think of the deleted chat records? How to restore the deleted chat records on wechat?
- 880. 索引处的解码字符串
- Xctf attack and defense world crypto advanced area
- Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?
- After getting these performance test decomposition operations, your test path will be more smooth
- Sword finger offer 22 The penultimate node in the linked list
- How does WPS cancel automatic numbering? Four options
猜你喜欢

Intelligent deodorizer embedded development

MySQL日志管理、数据备份、恢复

Do you know how to show the health code in only 2 steps

【LeetCode】236. Nearest common ancestor of binary tree

At the age of 32, I fell into a middle-aged crisis and finally quit naked...

Solidy - fallback function - 2 trigger execution modes

MySQL高级SQL语句

Strlen and sizeof, array length and string length, don't be silly

MySQL log management, data backup and recovery

Solidity - Security - reentrancy attack
随机推荐
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
Sword finger offer 22 The penultimate node in the linked list
Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"
Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
SSL证书续费相关问题详解
CompletableFuture从了解到精通,你想知道的这里都有
Inno setup the simplest user-defined interface effect
Dynamic programming -- gliding wing of the strange thief Kidd
Sword finger offer 29 Print matrix clockwise
46. 全排列-dfs双百代码
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
ES6扩展运算符(...)
InputStream转InputStreamSource
Summation of basic exercise sequence of test questions
Projet Web de déploiement du serveur Cloud
观察者模式、状态模式在实际工作中的使用
Solidity - 安全 - 重入攻击(Reentrancy)
Navigate back to fragmentpageradapter - & gt; Fragment is empty - navigating back to fragmentpageradapter - & gt; fragments are empty
Leetcode search insert location