package test
import "testing"
func TestMyFirst(t *testing.T) {
t.Log("My First Test")
}
编写测试程序注意:
- 源码文件需以_test.go结尾:xxx_test.go
- 测试方法名以Test开头:func TestXXX(t *testing.T){…}
package test
import "testing"
func TestFibonacci(t *testing.T) {
//定义变量
//var a int = 1
//var b int = 1
//var (
// a int = 1
// b = 1
//)
a := 1
b := 1
t.Log(a)
for i := 0; i < 5; i++ {
t.Log(b)
tmp := a
a = b
b = tmp + a
}
}
func TestExchange(t *testing.T) {
a:=1
b:=2
//tmp:=a
//a=b
//b=tmp
//一个语句可赋值多个变量
a,b = b,a
t.Log(a,b)
}
变量赋值不同的地方
- 赋值可自动类型判断
- 一个语句可赋值多个变量
常量定义:
go语言可以快速设置连续值
package test
import "testing"
const(
Monday = 1+iota
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
)
const(
Readable = 1<<iota
Writable
Excutable
)
func TestConstant(t *testing.T) {
t.Log(Monday,Friday)
a:=1 //0001
t.Log(a&Readable==Readable)
}