mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
06.3
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
|
|
||||||
## 翻译进度
|
## 翻译进度
|
||||||
|
|
||||||
6.2 [参数与返回值](eBook/06.2.md)
|
6.3 [传递变长参数](eBook/06.3.md)
|
||||||
|
|
||||||
## 支持本书
|
## 支持本书
|
||||||
|
|
||||||
|
122
eBook/06.3.md
122
eBook/06.3.md
@@ -1,19 +1,107 @@
|
|||||||
## 啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0
|
|
||||||
|
|
||||||
要不等到 **2014 年 7 月 12 日** 再来看看吧~~
|
|
||||||
|
|
||||||
这里还有一些其它的学习资源噢~
|
|
||||||
|
|
||||||
- [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming)
|
|
||||||
- [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang)
|
|
||||||
- [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases)
|
|
||||||
|
|
||||||
神马?你说你不想学习?那好吧,去逛逛看看行情也行~
|
|
||||||
|
|
||||||
- [Go Walker](https://gowalker.org) **Go 项目 API 文档在线浏览工具**
|
|
||||||
- [Go 中国社区](http://bbs.go-china.org)
|
|
||||||
- [Go语言学习园地](http://studygolang.com/)
|
|
||||||
- [Golang中国](http://golangtc.com)
|
|
||||||
|
|
||||||
# 6.3 传递变长参数
|
# 6.3 传递变长参数
|
||||||
|
|
||||||
|
如果函数的最后一个参数是采用 `...type` 的形式,那么这个函数就可以处理一个变长的参数,这个长度可以为 0,这样的函数称为变参函数。
|
||||||
|
|
||||||
|
func myFunc(a, b, arg ...int) {}
|
||||||
|
|
||||||
|
这个函数接受一个类似某个类型的 slice 的参数(详见第 7 章),该参数可以通过第 5.4.4 章中提到的 for 循环结构迭代。
|
||||||
|
|
||||||
|
示例函数和调用:
|
||||||
|
|
||||||
|
func Greeting(prefix string, who ...string)
|
||||||
|
Greeting("hello:", "Joe", "Anna", "Eileen")
|
||||||
|
|
||||||
|
在 Greeting 函数中,变量 `who` 的值为 `[]string{"Joe", "Anna", "Eileen"}`。
|
||||||
|
|
||||||
|
如果参数被存储在一个数组 `arr` 中,则可以通过 `arr...` 的形式来传递参数调用变参函数。
|
||||||
|
|
||||||
|
示例 6.7 varnumpar.go
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
x := Min(1, 3, 2, 0)
|
||||||
|
fmt.Printf("The minimum is: %d\n", x)
|
||||||
|
arr := []int{7,9,3,5,1}
|
||||||
|
x = Min(arr...)
|
||||||
|
fmt.Printf("The minimum in the array arr is: %d", x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Min(a ...int) int {
|
||||||
|
if len(a)==0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
min := a[0]
|
||||||
|
for _, v := range a {
|
||||||
|
if v < min {
|
||||||
|
min = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
输出:
|
||||||
|
|
||||||
|
The minimum is: 0
|
||||||
|
The minimum in the array arr is: 1
|
||||||
|
|
||||||
|
**练习 6.3** varargs.go
|
||||||
|
|
||||||
|
写一个函数,该函数接受一个变长参数并对每个元素进行换行打印。
|
||||||
|
|
||||||
|
一个接受变长参数的函数可以将这个参数作为其它函数的参数进行传递:
|
||||||
|
|
||||||
|
```go
|
||||||
|
function F1(s … string) {
|
||||||
|
F2(s …)
|
||||||
|
F3(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func F2(s … string) { }
|
||||||
|
func F3(s []string) { }
|
||||||
|
```
|
||||||
|
|
||||||
|
变长参数可以作为对应类型的 slice 进行二次传递。
|
||||||
|
|
||||||
|
但是如果变长参数的类型并不是都相同的呢?使用 5 个参数来进行传递并不是很明智的选择,有 2 种方案可以解决这个问题:
|
||||||
|
|
||||||
|
1. 使用结构(详见第 10 章):
|
||||||
|
|
||||||
|
定义一个结构类型,假设它叫 `Options`,用以存储所有可能的参数:
|
||||||
|
|
||||||
|
```go
|
||||||
|
type Options struct {
|
||||||
|
par1 type1,
|
||||||
|
par2 type2,
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
函数 F1 可以使用正常的参数 a 和 b,以及一个没有任何初始化的 Options 结构: `F1(a, b, Options {})`。如果需要对选项进行初始化,则可以使用 `F1(a, b, Options {par1:val1, par2:val2})`。
|
||||||
|
|
||||||
|
2. 使用空接口:
|
||||||
|
|
||||||
|
如果一个变长参数的类型没有被指定,则可以使用默认的空接口 `interface{}`,这样就可以接受任何类型的参数(详见第 11.9 节)。该方案不仅可以用于长度未知的参数,还可以用于任何不确定类型的参数。一般而言我们会使用一个 for-range 循环以及 switch 结构对每个参数的类型进行判断:
|
||||||
|
|
||||||
|
```go
|
||||||
|
func typecheck(..,..,values … interface{}) {
|
||||||
|
for _, value := range values {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case int: …
|
||||||
|
case float: …
|
||||||
|
case string: …
|
||||||
|
case bool: …
|
||||||
|
default: …
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 链接
|
||||||
|
|
||||||
|
- [目录](directory.md)
|
||||||
|
- 上一节:[参数与返回值](06.2.md)
|
||||||
|
- 下一节:[defer 和追踪](06.4.md)
|
18
eBook/06.4.md
Normal file
18
eBook/06.4.md
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
## 啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0
|
||||||
|
|
||||||
|
要不等到 **2014 年 8 月 4 日** 再来看看吧~~
|
||||||
|
|
||||||
|
这里还有一些其它的学习资源噢~
|
||||||
|
|
||||||
|
- [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming)
|
||||||
|
- [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang)
|
||||||
|
- [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases)
|
||||||
|
|
||||||
|
神马?你说你不想学习?那好吧,去逛逛看看行情也行~
|
||||||
|
|
||||||
|
- [Go Walker](https://gowalker.org) **Go 项目 API 文档在线浏览工具**
|
||||||
|
- [Golang Home](http://golanghome.com)
|
||||||
|
- [Go 语言学习园地](http://studygolang.com/)
|
||||||
|
- [Golang 中国](http://golangtc.com)
|
||||||
|
|
||||||
|
# 6.4 defer 和追踪
|
@@ -49,6 +49,7 @@
|
|||||||
- 6.1 [介绍](06.1.md)
|
- 6.1 [介绍](06.1.md)
|
||||||
- 6.2 [参数与返回值](06.2.md)
|
- 6.2 [参数与返回值](06.2.md)
|
||||||
- 6.3 [传递变长参数](06.3.md)
|
- 6.3 [传递变长参数](06.3.md)
|
||||||
|
- 6.4 [defer 和追踪](06.4.md)
|
||||||
- 第7章:数组(array)与切片(slice)
|
- 第7章:数组(array)与切片(slice)
|
||||||
- 第8章:Maps
|
- 第8章:Maps
|
||||||
- 第9章:包(package)
|
- 第9章:包(package)
|
||||||
|
Reference in New Issue
Block a user