Go 語言變量及常量的定義與使用

變量的定義與使用

package fib_test

import (
    "fmt"
) //引入代碼依賴

func TestFibList(t *testing.T) {
    var a int = 1
    var b int = 1
    fmt.Print(a)
    
    for i := 0; i < 5; i++ {
        fmt.Print(" ", b)
        tmp := a
        a = b
        b = tmp + a
    }
    fmt.Println()
    t.Log("finish.")
}
// 交換兩個變量的值
func TestFibList(t *testing.T) {
    a := 1
    b := 1
    a, b = b, a
    t.Log(a, b)
}

常量的定義與使用

package constant_test

import (
    "fmt"
) //引入代碼依賴

const (
    Mon = iota + 1
    Tue
    Wed
)

// 位運算
const (
    Readable = 1 << iota
    Writable
    Executable
)

func TestConstant0(t *testing.T) {
    t.Log(Mon, Tue, Wed)
}

func TestConstant1(t *testing.T) {
    a := 1 //0001,可讀
    t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable)
    
    a := 7 //0111,可讀可寫可執行
    t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable)
}
相關文章
相關標籤/搜索