测试
- 源码文件为 xx_test.go
- 测试方法名以 Test 开头
- 使用 t.Log("xxx") ,可以在测试中输出内容
package try_test
import "testing"
func TestFirstTry(t *testing.T){
t.Log("My first try!")
}
变量赋值方式
- 变量有三种赋值方式
- Go 中具有一定的类型推断能力
package fib
import (
"testing"
)
func TestFbiList(t *testing.T) {
//var a int = 1
//var b int = 1
//a := 1
//b := 1
var (
a int = 1 // 指定字符类型
b = 1 // 自动判断类型
)
for i := 0 ; i < 5 ; i++{ // 在这行代码中,使用 i := 0 对 i 进行了赋值
t.Log(b)
tmp := a
a = b
b = tmp + a
}
}
func TestExchange(t *testing.T){
a := 1
b := 2
a, b = b, a
t.Log(a, b)
}
常量赋值
- 使用 const 进行常量赋值
- iota 可以实现递增,你甚至可以用它做位移动
package constant_test
import (
"reflect"
"testing"
)
const (
Monday = 10
Tuesday = iota + 1
Wednesday
)
const (
Readable = 1 << iota // 0001
Writable // 0010
Executable // 0100
)
func TestContantTry(t *testing.T){
t.Log(Monday, Tuesday, Wednesday) // 10,2,3
}
func TestConstantTry1(t *testing.T){
a := 7 // 0111
t.Log(reflect.TypeOf(Readable)) // int
t.Log(Readable, Writable, Executable) // 1,2,4
t.Log(a & Readable, a & Writable, a & Executable ) // 1,2,4
}
数据类型
- 数据类型和其他的编程语言类似
- Go 不允许任何类型的隐式类型转换
- 甚至别名和原有类型之间都无法进行转换
package type_test
import (
"math"
"testing"
)
type MyInt int64
func TestImplicit(t *testing.T){
var a int = 1
var b int64 = 64
//b = a 这行代码无法被被编译通过
b = int64(a)
t.Log(a, b)
var c MyInt
//c = b 这行代码也无法编译
c = MyInt(b)
t.Log(b, c)
}
func TestPoint(t *testing.T){
a := 1
aPtr := &a
//aPtr = aPtr + 1 无法编译
t.Log(a, aPtr) // 1,0xc00008a198
t.Logf("%T %T", a, aPtr) // int,*int
}
func TestMaxInt(t *testing.T){
t.Log(math.MaxInt16) //32767
}
- Go 的字符串初始化为空字符串,而不是空
- Go 的数字会被初始化为 0,而不是空
func TestString(t *testing.T) {
var s string
var i int
t.Log("-" + s + "-") // --
t.Log(len(s)) // 0
if s == ""{
t.Log("none") // 输出 none ,说明 s 就是一个空字符串,而非一个空指针
}
t.Log(i) // 0
}
运算符
- 常见的算数运算符号:+、-、*、/、%、++、--
- 比较运算符中, == 判断数组有两个限制:长度 和 维数必须相同
package operatro_test
import "testing"
const (
Readable = 1 << iota
Writable
Executable
)
func TestCompareArray(t *testing.T){
a := [...]int{1,2,3}
b := [...]int{1,2,3}
c := [...]int{1,4,3}
//d := [...]int{1,2,3,4}
t.Log(a == b)
t.Log(a == c)
//t.Log(a == d) 在 Go 中,这行代码无法编译
}
- 逻辑运算符:&&,||,!
- 位运算符:&,|,,<<,>>,特别地,& 为按位置零:右边为 1,必定置零,右边为 0,结果为左边
func TestBitClear(t *testing.T) {
a := 7 // 0111
a = a &^ Readable // 0110
a = a &^ Writable // 0100
t.Log(a) // 4
t.Log(Readable, Writable, Executable) // 1 2 4
t.Log(a & Readable, a & Writable, a & Executable) // 0 0 4
}
条件语句
- if 的条件必须为 bool 类型
- if 可以做一个初始赋值操作,这个操作在 Go 中其实有很多用处,在之后你会感受到它的好处
package condition
import "testing"
func TestIfMultSec(t *testing.T) {
if a := true; a{
t.Log("OK", )
}
if _, err := true,true; err == true{
t.Log("True")
}
}
- switch 后面可以接多个条件,使用逗号作为分隔
- 每个 case 后会自动 break,所以你不需要手动实现
func TestSwitch(t *testing.T){
for i := 0; i < 5; i++{
switch i{
case 1, 3:
t.Log("Odd")
case 0, 2:
t.Log("Even")
default:
t.Log("UnKnow")
}
switch {
case i % 2 == 1:
t.Log("Odd")
case i % 2 == 0:
t.Log("Even")
default:
t.Log("UnKnow")
}
}
}
循环语句
- 所有的循环都是使用 for 完成,你可以将它当成 while
package loop
import "testing"
func TestWhileLoop(t *testing.T) {
n := 1
for n < 5 {
t.Log(n)
n++
}
for i := 1 ; i <= 5 ; i++ {
t.Log(i)
}
for {
t.Log('h') // 这是一个死循环
}
}
网友评论