DEV Community

Cover image for Go的基本操作(一)
JU DaDao
JU DaDao

Posted on • Updated on

Go的基本操作(一)

基本儲存型態、變數以及迴圈用法

數值計算

  • 各型態

    • string, integer, floats, booleans, booleans operaters
package main

      import "fmt"

      func main() {

          fmt.Println("go" + "lang")

          fmt.Println("1+1 =", 1+1)
          fmt.Println("7.0/3.0 =", 7.0/3.0)

          fmt.Println(true && false)
          fmt.Println(true || false)
          fmt.Println(!true)
      }
Enter fullscreen mode Exit fullscreen mode
- string可以直接做相加合併,其他如同C/C++規則
Enter fullscreen mode Exit fullscreen mode
  • 變數宣告使用

    package main

    import "fmt"

    func main() {

        var a = "initial"
        fmt.Println(a)

        var b, c int = 1, 2
        fmt.Println(b, c)

        var d = true
        fmt.Println(d)

        var e int
        fmt.Println(e)

        f := "apple"
        fmt.Println(f)
    }

Enter fullscreen mode Exit fullscreen mode
  • 可以一次宣告多個變數。且比較特殊的是最後的:=。他是一個簡化的宣告,可以依據後方的內容去決定var的型態。

    • const用法

    package main

    import (
        "fmt"
        "math"
    )

    const s string = "constant"

    func main() {
        fmt.Println(s)

        const n = 500000000

        const d = 3e20 / n
        fmt.Println(d)

        fmt.Println(int64(d))

        fmt.Println(math.Sin(n))
    }
Enter fullscreen mode Exit fullscreen mode
  • 可以宣告在各 var的位置前。但是在指定變數型態之前都是不確定型態的,如最後的d

    fmt.Println(d)
    //6e+11
    fmt.Println(int64(d))
    //600000000000
Enter fullscreen mode Exit fullscreen mode
  • For迴圈
    package main

    import "fmt"

    func main() {

        i := 1
        for i <= 3 {
            fmt.Println(i)
            i = i + 1
        }

        for j := 0; j < 3; j++ {
            fmt.Println(j)
        }

        for i := range 3 {
            fmt.Println("range", i)
        }

        for {
            fmt.Println("loop")
            break
        }

        for n := range 6 {
            if n%2 == 0 {
                continue
            }
            fmt.Println(n)
        }
    }
Enter fullscreen mode Exit fullscreen mode
  • 這邊介紹了幾種for迴圈的方式,與C/c++不同不需要"()",且也支援range。還有一個用法是倒數遞二種,for 然後沒有接任何condition的用法。他會執行到讀取到break為止。在示範中print一次"loop"字串就結束了。

    • IF/else 用法跟 c 一樣
    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 divisible by 4")
        }

        if 8%2 == 0 || 7%2 == 0 {
            fmt.Println("either 8 or 7 are even")
        }

        if num := 9; num < 0 {
            fmt.Println(num, "is negative")
        } else if num < 10 {
            fmt.Println(num, "has 1 digit")
        } else {
            fmt.Println(num, "has multiple digits")
        }
    }
Enter fullscreen mode Exit fullscreen mode
  • swith 用法也與C 相同
    package main

    import (
        "fmt"
        "time"
    )

    func main() {

        i := 2
        fmt.Print("Write ", i, " as ")
        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 the weekend")
        default:
            fmt.Println("It's a 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("I'm a bool")
            case int:
                fmt.Println("I'm an int")
            default:
                fmt.Printf("Don't know type %T\n", t)
            }
        }
        whatAmI(true)
        whatAmI(1)
        whatAmI("hey")
    }
Enter fullscreen mode Exit fullscreen mode

參考網站: gobyexample

Top comments (0)