Go 語言中的運算符

運算符

Go 語言沒有前置的 ++ 和 -- 運算符。code

package operator_test

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

func TestCompareArray(t *testing.T) {
    a := [...]int{1, 2, 3, 4}
    b := [...]int{1, 3, 2, 4}
    c := [...]int{1, 2, 3, 4, 5}
    d := [...]int{1, 2, 3, 4}
    
    t.Log(a == b) //false
    t.Log(a == c) //編譯報錯,長度不一致
    t.Log(a == d) //true
}

按位清零運算符。it

const (
    Readable = 1 << iota
    Writable
    Executable
)

func TestBitClear(t *testing.T) {
    a := 7               //0111
    a = a &^ Readable    //清除讀權限
    a = a &^ Executable  //清除執行權限
    t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable)
}
相關文章
相關標籤/搜索