【JKSJ49-2】测试程序,变量和常量

91 阅读1分钟

编写测试程序

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

变量赋值

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

如以下代码所示:

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

常量赋值

1.支持快速设置连续值

如以下代码所示:

package constant

import "testing"

const (
   Monday = iota + 1
   Tuesday
   Wednesday
)

const (
   Readable = 1 << iota
   Writable
   Executable
)

func TestConstant1(t *testing.T) {
   t.Log(Monday, Tuesday, Wednesday)
}

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