Skip to content

Commit

Permalink
multiple return values;variadic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang committed Jan 27, 2018
1 parent 9367f2f commit 5405cef
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions PROGRESS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
- [x] Maps->关联数组
- [x] Range->Range 遍历
- [x] Functions->函数
- [ ] Multiple Return Values->多返回值
- [ ] Variadic Functions->变参函数
- [x] Multiple Return Values->多返回值
- [x] Variadic Functions->变参函数
- [ ] Closures->闭包
- [ ] Recursion->递归
- [ ] Pointers->指针
Expand Down
10 changes: 3 additions & 7 deletions examples/multiple-return-values/multiple-return-values.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Go 内建_多返回值_ 支持。这个特性在 Go 语言中经常被用到,
// Go 内建_多返回值_支持。这个特性在 Go 语言中经常被用到,
// 例如用来同时返回一个函数的结果和错误信息。

package main
Expand All @@ -12,16 +12,12 @@ func vals() (int, int) {

func main() {

// 这里我们通过_多赋值_ 操作来使用这两个不同的返回值
// 这里我们通过_多赋值_操作来使用这两个不同的返回值
a, b := vals()
fmt.Println(a)
fmt.Println(b)

// 如果你仅仅想返回值的一部分的话,你可以使用空白定
// 义符 `_`。
// 如果你仅仅需要返回值的一部分的话,你可以使用空白标识符`_`。
_, c := vals()
fmt.Println(c)
}

// todo: named return parameters
// todo: naked returns
4 changes: 2 additions & 2 deletions examples/multiple-return-values/multiple-return-values.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
8aa7d7a915c4b730aa1c42d2a6a7a1bcca9e4db6
5C9fEBil8L
dc382b645820d4a6543b37e933c747899ef0ca60
v5_0zLKWgzQ
2 changes: 1 addition & 1 deletion examples/multiple-return-values/multiple-return-values.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ $ go run multiple-return-values.go
7
7

# 允许可变长参数是 Go 函数的另一个好的特性;我们将在接下来
# 允许可变长参数是 Go 函数另一个很好的特性;我们将在接下来
# 进行学习。
Binary file removed examples/stateful-goroutines/stateful-goroutines
Binary file not shown.
4 changes: 2 additions & 2 deletions examples/stateful-goroutines/stateful-goroutines.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
9a93bfcf2cd82f53d65cf991c89051e07001d818
pN-hufwq4T
48520fefdb7064ad9c0f2c6910ff66ae6d56515e
cm1oRLA1mTN
10 changes: 5 additions & 5 deletions examples/variadic-functions/variadic-functions.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// [_可变参数函数_](http://zh.wikipedia.org/wiki/可變參數函數)。可以用任意
// 数量的参数调用。例如,`fmt.Println` 是一个常见的变参函数。
// [_可变参数函数_](http://zh.wikipedia.org/wiki/可變參數函數)。在调用时可以用任意数量的参数。
// 例如,`fmt.Println` 是一个常见的变参函数。

package main

import "fmt"

// 这个函数使用任意数目的 `int` 作为参数。
// 这个函数接受任意数目的 `int` 作为参数。
func sum(nums ...int) {
fmt.Print(nums, " ")
total := 0
Expand All @@ -17,11 +17,11 @@ func sum(nums ...int) {

func main() {

// 变参函数使用常规的调用方式,除了参数比较特殊
// 变参函数使用常规的调用方式,传入独立的参数
sum(1, 2)
sum(1, 2, 3)

// 如果你的 slice 已经有了多个值,想把它们作为变参
// 如果你有一个含有多个值的 slice,想把它们作为参数
// 使用,你要这样调用 `func(slice...)`。
nums := []int{1, 2, 3, 4}
sum(nums...)
Expand Down
4 changes: 2 additions & 2 deletions examples/variadic-functions/variadic-functions.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
a5a3f1c9aa8ca4fb32092a0c1b69e67e8386ee83
cqmxLtY8oY
6a5add504e1c66ee36df072b3e6b615a171a343d
0WdwoPEe0U8

0 comments on commit 5405cef

Please sign in to comment.