2022年2月9日 星期三

Go lang 學習筆記 - 11 Range

 ```

package main


import "fmt"


func main() {

        nums := []int{2, 3, 4}

        sum := 0

        for idx, num := range nums {

                sum += num

                fmt.Println("idx, val: ", idx, num)

        }

        fmt.Println("sum: ", sum)


        kvs := map[string]string{"a": "apple", "b": "banana"}

        for k,v := range kvs {

                fmt.Println("%s -> %s", k, v)

        }


        for k := range kvs {

                fmt.Println("key: ", k)

        }


        for i, c := range "go" {

                fmt.Println(i, c)

        }

}

```

Go 裡面 Range 的用法跟 Python 的 enumerate 有點像

如果是普通的 array 會一個一個列舉而且有 index

如果是 str 的話  value 會是 unicode 

執行結果:

idx, val:  0 2

idx, val:  1 3

idx, val:  2 4

sum:  9

%s -> %s a apple

%s -> %s b banana

key:  a

key:  b

0 103

1 111

2022年2月8日 星期二

Go lang 學習筆記 - 10 Maps

 ```

package main


import "fmt"


func main() {

        m := make(map[string]int)


        m["k1"] = 7

        m["k2"] = 12


        fmt.Println("map: ", m)


        v1 := m["k1"]

        fmt.Println("v1: ", v1)


        fmt.Println("len: ", len(m))


        delete(m, "k2")

        fmt.Println("map: ", m)


        _, present := m["k2"]

        fmt.Println("present: ", present)


        n := map[string]int{"foo": 1, "bar": 2}

        fmt.Println("map n: ", n)

}

```

Map 的用法跟 Python 的 dict 差不多

只是寫法不太一樣

比較特別的是 拿值的時候有第二個回傳值

可以用來判斷 key 有沒有在map裡面

執行結果:

map:  map[k1:7 k2:12]

v1:  7

len:  2

map:  map[k1:7]

present:  false

map n:  map[bar:2 foo:1]

2022年2月7日 星期一

Go lang 學習筆記 - 9 Slices

 ```

package main


import "fmt"


func main() {

        s := make([]string, 3)

        s[0] = "a"

        s[1] = "b"

        s[2] = "c"


        fmt.Println("set: ", s)

        fmt.Println("get: ", s[2])

        fmt.Println("len: ", len(s))


        s = append(s, "d")

        s = append(s, "e", "f")

        fmt.Println("apd: ", s)


        c := make([]string, len(s))

        copy(c, s)

        fmt.Println("copy: ", c)


        l := s[2:5]

        fmt.Println("slic1: ", l)


        l = s[:5]

        fmt.Println("slic2: ", l)


        l = s[2:]

        fmt.Println("slic3: ", l)

        twoD := make([][]int, 3)

        for i := 0; i < 3; i++ {

                innerLen := i + 1

                twoD[i] = make([]int, innerLen)

                for j := 0; j < innerLen; j++ {

                        twoD[i][j] = i + j

                }

        }

        fmt.Println("2D: ", twoD)

}

```

Slice 的操作跟 Python 的用法很相似

比較特別的是用 make 先產生固定長度的 array (有點像是 C++ 的 new 但不需要delete )

然後在二維陣列(2D array)的 make 第二個參數指定的只有第一層的長度

內層 array 的長度可以不一樣


執行結果:

set:  [a b c]

get:  c

len:  3

apd:  [a b c d e f]

copy:  [a b c d e f]

slic1:  [c d e]

slic2:  [a b c d e]

slic3:  [c d e f]

2D:  [[0] [1 2] [2 3 4]]

2022年2月6日 星期日

Go lang 學習筆記 - 8 Arrays

``` 

package main


import "fmt"


func main() {

        var a [5]int

        fmt.Println(a)


        a[4] = 100

        fmt.Println("set: ", a)

        fmt.Println("get: ", a[4])


        fmt.Println("len: ", len(a))


        b := [5]int{1, 2, 3, 4, 5}

        fmt.Println("decl: ",b )


        var twoD [2][3]int

        fmt.Println("2D arr: ", twoD)

        for i := 0; i < 2; i++ {

                for j := 0; j < 3; j++ {

                        twoD[i][j] = i + j

                }

        }

        fmt.Println("2D arr: ", twoD)

}

```

Array 的語法跟 C 不一樣的地方是把 維度放在型別前面

而 C 是放在 identifier 後面


執行結果:

[0 0 0 0 0]

set:  [0 0 0 0 100]

get:  100

len:  5

decl:  [1 2 3 4 5]

2D arr:  [[0 0 0] [0 0 0]]

2D arr:  [[0 1 2] [1 2 3]]

2022年2月5日 星期六

Go lang 學習筆記 - 7 Switch

 

```

package main


import (

        "fmt"

        "time"

)


func main() {

        i := 2

        switch i {

        case 1:

                fmt.Println("one")

        case 2:

                fmt.Println("two")

        case 3:

                fmt.Println("three")

        }


        switch time.Now().Weekday() {

        case time.Saturday, time.Sunday:

                fmt.Println("It's weekend.")

        default:

                fmt.Println("It's weekday.")

        }


        t := time.Now()

        switch {

        case t.Hour() < 12:

                fmt.Println("It's before noon")

        default:

                fmt.Println("It's after noon")

        }


        whatAmI := func(i interface{}) {

                switch t := i.(type) {

                case bool:

                        fmt.Println("It's bool type")

                case int:

                        fmt.Println("It's int type")

                default:

                        fmt.Printf("Don't unknown type %T\n", t)

                }

        }

        whatAmI(true)

        whatAmI(6)

        whatAmI("hey")

}

```

Switch 的用法跟 C 也是差不多

只是範例中多了 i.(type) 的用法來得到變數(可能也有常數)的型別(type)

執行結果:

two

It's weekday.

It's after noon

It's bool type

It's int type

Don't unknown type string

2022年1月31日 星期一

Go lang 學習筆記 - 6 If/Else

 這篇是練習 If-else 的用法 

```

package main


import "fmt"


func main() {

        if 7 % 2 == 0 {

                fmt.Println("7 is even")

        } else {

                fmt.Println("7 is odd")

        }


        if 8 % 4 == 0 {

                fmt.Println("8 is even")

        } else {

                fmt.Println("8 is odd")

        }


        if n := 9; n > 0 {

                fmt.Println("n is positive")

        } else if n < 0 {

                fmt.Println("n is negative")

        } else {

                fmt.Println("n is zero")

        }

}

```

比較特別的是可以用分號斷行

而且在condition statement (條件判斷式) 宣告的變數

可以在同一個If else裡面繼續使用

執行結果:

7 is odd

8 is even

n is positive

2022年1月30日 星期日

Go lang 學習筆記 - 5 For 迴圈

 練習基本的 for loop

```

package main


import "fmt"


func main() {

        i := 1

        for i < 3 {

                fmt.Println(i)

                i++

        }


        for j := 7; j < 10; j++ {

                fmt.Println(j)

        }


        for {

                fmt.Println("Infinitely loop")

                break

        }


        for n := 0; n < 5; n++ {

                if n % 2 == 0 {

                        continue

                }

                fmt.Println(n)

        }

}

```

Go lang 裡面的 for loop 不用小括號

但中間的分號用法是一樣的

輸出結果:

1

2

7

8

9

Infinitely loop

1

3

Go lang 學習筆記 - 17 Pointers

``` package main import "fmt" func zeroval(n int) {         n = 0 } func zeroptr(n *int) {         *n = 0 } func main() {         ...