Go语言学习笔记-基本程序结构-变量常量

变量

var关键字定义变量

  1. 赋值可以自动类型推断
  2. 在一个赋值语句中可以对多个变量同时进行赋值

package fib
import "testing"

func TestFibList(t *testing.T) {
        //var a int = 1
        //var b int = 1
        var (
                a = 1
                //a int = 1
                b = 1
                //b int = 1
        )
        t.Log(a)
        for i:=0; i<5; i++ {
                t.Log(b)
                //tmp:=a
                //a=b
                //b=tmp+b
                a,b = b,a+b
        }

}

func TestExchange(t *testing.T) {
        a := 1
        b := 2
        //tmp := a
        //a = b
        //b = tmp
        a,b = b,a
        t.Log(a,b)
}

常量

快速设置连续值
package constant_test
import "testing"

const(
        Monday = iota + 1
        Tuesday
        Wednesday
        Thursday
        Friday
        Staturday
        Sunday

)

const (
        Readable = 1 << iota
        Writable
        Executable

)

func TestConstantWeek(t *testing.T) {
        t.Log(Monday,Tuesday,Wednesday,Sunday)

}

func TestConstantBit(t *testing.T) {
        a := 2
        t.Log(a & Readable == Readable,a & Writable == Writable, a & Executable == Executable)
}

你可能感兴趣的:(Go语言学习笔记-基本程序结构-变量常量)