1.Range
Range iterate over elements in a variaety of data structure.Let's how to use range with some of the data struectures we've already learned.
[maxwell@oracle-db-19c Day02]$ vim range.go
[maxwell@oracle-db-19c Day02]$ cat range.go
package main
import "fmt"
func main(){
nums := []int{2, 3, 4}
sum := 0
for _,num := range nums {
sum += num
}
fmt.Println("sum:",sum)
for i,num := range nums {
if num == 3 {
fmt.Println("index:", i)
}
}
kvs := map[string]string{"a": "apple", "b":"banana"}
for k,v := range kvs{
fmt.Printf("%s -> %s\n", k,v)
}
for k := range kvs {
fmt.Println("key:", k)
}
for i, c := range "go" {
fmt.Println(i, c)
}
}
[maxwell@oracle-db-19c Day02]$ go run range.go
sum: 9
index: 1
a -> apple
b -> banana
key: a
key: b
0 103
1 111
[maxwell@oracle-db-19c Day02]$
2. Functions
Functions are central in Go. We'll learn about functions with a few different examples.
[maxwell@oracle-db-19c Day02]$ vim functions.go
[maxwell@oracle-db-19c Day02]$ cat functions.go
package main
import "fmt"
func plus (a int, b int) int {
return a + b
}
func plusPlus(a, b, c int) int {
return a + b + c
}
func main() {
res := plus(1, 2)
fmt.Println("1+2 =", res)
res = plusPlus(1, 2, 3)
fmt.Println("1+2+3 =", res)
}
[maxwell@oracle-db-19c Day02]$ go run functions.go
1+2 = 3
1+2+3 = 6
[maxwell@oracle-db-19c Day02]$
3.Multiple Return Values
Go has built-in support for multiple return values.This feature is used often in idiomatic Go.
The (int,int) in this function signature shows that the function returns 2 ints.
[maxwell@oracle-db-19c Day02]$ vim mulreva.go
[maxwell@oracle-db-19c Day02]$ cat mulreva.go
package main
import "fmt"
func vals()(int, int) {
return 3, 7
}
func main() {
a,b := vals()
fmt.Println(a)
fmt.Println(b)
_, c := vals()
fmt.Println(c)
d,_ := vals()
fmt.Println(d)
}
[maxwell@oracle-db-19c Day02]$ go run mulreva.go
3
7
7
3
[maxwell@oracle-db-19c Day02]$
4. Variadic Functions
| Variadic functions can be called with any number of trailing arguments. For example, |
[maxwell@oracle-db-19c Day02]$ vim variadicfuc.go
[maxwell@oracle-db-19c Day02]$ cat variadicfuc.go
package main
import "fmt"
func sum(nums ... int){
fmt.Print(nums, " ")
total := 0
for _, num := range nums {
total += num
}
fmt.Println(total)
}
func main() {
sum(1, 2)
sum(1, 2, 3)
nums := []int{1, 2, 3, 4}
sum(nums...)
}
[maxwell@oracle-db-19c Day02]$ go run variadicfuc.go
[1 2] 3
[1 2 3] 6
[1 2 3 4] 10
[maxwell@oracle-db-19c Day02]$
5.Closures
Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
[maxwell@oracle-db-19c Day02]$ vim closures.go
[maxwell@oracle-db-19c Day02]$ cat closures.go
package main
import "fmt"
func intSeq() func() int {
i := 0
return func() int {
i++
return i
}
}
func main() {
nextInt := intSeq()
fmt.Println(nextInt())
fmt.Println(nextInt())
fmt.Println(nextInt())
newInts := intSeq()
fmt.Println(newInts())
}
[maxwell@oracle-db-19c Day02]$ go run closures.go
1
2
3
1
[maxwell@oracle-db-19c Day02]$
171

被折叠的 条评论
为什么被折叠?



