您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
golang不定长传参,三个点... 可以很灵活的传入多个参数,给两个例子,一看就明白了
package main
import "fmt"
func main() {
fmt.Println(Sum(1, 4, 6, 7, 8, 9, 100, 200))
}
func Sum(numbers ...int) int {
result := 0
for _, n := range numbers {
result += n
}
return result
}
go run test1.go
335
package main
import "fmt"
func main() {
fmt.Println(SumMap(map[string]string{"Italy": "Rome"}, map[string]string{"Japan": "Tokyo"}, map[string]string{"china": "beijing"}))
}
func SumMap(countryCapitalMaps ...map[string]string) map[string]string {
var result map[string]string
result = make(map[string]string)
//注意:这里需要先把多个map的顺序取出,否则会报错
//cannot use k (type int) as type string in map index
//cannot use v (type map[string]string) as type string in assignment
for i, num := range countryCapitalMaps {
fmt.Println(i)
fmt.Println(num)
for k, v := range num {
result[k] = v
}
}
return result
}
go run test2.go //这里会打印map的顺序号和值
0
map[Italy:Rome]
1
map[Japan:Tokyo]
2
map[china:beijing]
map[Italy:Rome Japan:Tokyo china:beijing]
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。