02-变量、常量以及与其他语言的差异

43 阅读1分钟

The master has failure more than the beginner has tried

编写测试程序

  1. 源码文件以_test结尾:xxx_test.go
  2. 测试方法名以Test开头:func TestXXX(t *testing.T) {...}

实现一个菲波那切数列

package fib

import (
    "testing"
)

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

变量赋值

  • 赋值可以进行自动类型推断
  • 在一个赋值语句中可以对多个变量进行同时赋值
func TestExchange(t *testing.T) {
    a := 1
    b := 2
    // temp := a
    // a = b
    // b = temp
    a, b = b, a
    t.Log(a, b)
}

常量定义

const (
    Readable = 1 << iota
    Writeable
    Excutable
)

func TestConstantTry1 (t *testing.T){
    a := 1
    t.Log(a & Readable == Readable, a & Writeable == Writeable, a & Excutable == Excutable)
}

这里需要补课,位运算到底是怎么计算的