Go语言学习 Day01 Summary

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, fmt.Println is a common variadic function.

[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]$ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值