Variables and Constants
1. Write a test program
- Source code files
_test
end with:xxx_test.go
- Test method names
Test
start with:func TestXXX(t *testing.T) {...}
package try_test
import "testing"
func TestFirstTry(t *testing.T) {
t.Log("My first try!")
}
> go test first_test.go
=== RUN TestFirstTry
first_test.go:6: My first try!
--- PASS: TestFirstTry (0.00s)
ok command-line-arguments 0.056s
PASS
go test related issues
//注意:go test -v xxx_test.go 才能输出 t.Log 里的文字,多数IDE里这个参数是默认打开的,所以在IDE里你直接可以看到输出。命令行要加这个参数或者修改默认配置。
//如果你使用的是 vs code 编辑器的话,安装好了go的相关配置,会在测试文件中看到 ` run test | debug test `的提示
//你点击 `run test` 之后在下面看不到 `t.Log()` 输出的内容就请把 `"go.testFlags": ["-v"],` 这行代码加入到你的 settings.json 文件中(设置 Workspace Settings。打开 .vscode/settings.json)
Executing test reports an error package ch2/test is not in GOROOT (D:\Go\src\ch2\test) (the test package is not in Go’s default package path (GOROOT))
//在settings.json 中加入下面内容,修改GOPATH 目录,
//注意在 Windows 中,目录路径中的反斜杠字符 \ 需要用双反斜杠 \\ 来转义
"go.testEnvVars": {
"GOPATH": "D:\\Gocode"
},
//在cmd执行
go env -w GO111MODULE=off
//重启vscode 再点击run test | debug test 发现成功
2. Implement Fibonacci sequence
1, 1, 2, 3, 5, 8, 13, ….
package fib
import (
"fmt"
"testing"
)
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()
}
//运行测试结果
=== RUN TestFibList
1 1 2 3 5 8
--- PASS: TestFibList (0.00s)
PASS
ok ch2/fib (cached)
3. Variable assignment
Differences from other major programming languages
-
Assignment enables automatic type inference
-
Multiple variables can be assigned simultaneously in one assignment statement.
The var keyword is used to assign values to variables.
Assignment can perform automatic type inference.
// var的关键字用于给变量赋值,类型声明放到变量名的后面
var a int = 1
var b int = 1
// 可以简写为
var (
a int = 1
b int = 1
)
//go 具有一定的默认推断能力,如果使用默认类型,还可以这么写
var (
a int = 1
b = 1
)
// go 里面还可以直接不需要赋值关键字,使用类型推断,直接给变量进行初始化和赋值
a := 1
b := 1
//也可以在前面声明一个变量的类型。在后面进行赋值(通常这种变量会用在全局或者外部的变量)
var a int
func TestFibList(t *testing.T) {
a = 1
}
Multiple variables can be assigned simultaneously in one assignment statement.
//交换两个变量的值
func TestExchang(t *testing.T) {
a := 1
b := 2
tmp := a
a = b
b = tmp
t.Log(a, b)
}
//在go中有更简洁的方式
func TestExchang(t *testing.T) {
a := 1
b := 2
// tmp := a
// a = b
// b = tmp
a, b = b, a
t.Log(a, b)
}
fmt.Print to output our value, t.Log is often used to replace it in unit testing
package fib
import (
"testing"
)
func TestFibList(t *testing.T) {
var a int = 1
var b int = 1
t.Log(a)
for i := 0; i < 5; i++ {
t.Log(" ", b)
tmp := a
a = b
b = tmp + a
}
}
4. Constant definition
Differences from other major programming languages
- Quickly set continuous values
To define 7 days a week, there is no need to assign a separate value to each constant in Go. You can achieve incremental +1 by writing as follows
package constant_test
import (
"testing"
)
const (
Monday = iota + 1
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
// Monday = 1
// Tuesday = 2 依次类推,这种写法也是可以的
)
func TestConstantTry(t *testing.T) {
t.Log(Monday, Wednesday, Thursday, Friday, Saturday, Sunday)
}
//RUN TEST
=== RUN TestConstantTry
D:\XXXX\XXXX\XXXX\XXXXXX\constant_try_test.go:18: 1 3 4 5 6 7
--- PASS: TestConstantTry (0.00s)
PASS
ok ch2/constant_test (cached)
Go can continuously define some bit constants
const (
Readable = 1 << iota
Writable
Executable
)
func TestConstantTry1(t *testing.T) {
a := 7 //二进制位是0111
t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable)
}
// a := 7 ///二进制位是0111 RUN TEST 得到结果true true true
// 修改a := 1 ///二进制位是0001 ,run test 得到结果 true false false
Go can continuously define some bit constants
When the first bit is 1 and the other bits are 0, we mean Readable
When the second bit is 1 and the other bits are 0, we mean Writable
When the third bit is 1 and the other bits are 0, we indicate Executable
This code defines three constants Readable, Writable and Executable, their values are 1 left shift 0, 1 left shift 1 and 1 left shift 2 respectively. This usage is called a "bit operator" or "bit mask", which sets different binary bits to 1 or 0 to represent different attributes or flags. Initialize the integer variable a to 7, whose binary representation is 0111. Then bitwise AND operation.