1 回答
TA贡献1921条经验 获得超9个赞
First是一个关于 type 的方法Add。例如,
a := Add{}
a.First(x)
addition.go
package calculator
type Add struct{}
func BasicAddition(x int) int { // this won't be in the final release
return x + 2
}
func (h Add) First(x int) int {
x += 5
return x
}
func (h Add) Second(x int) int {
x += 10
return x
}
addition_test.go
package calculator
import "testing"
func TestBasicAddition(t *testing.T) {
x := 30
if y := BasicAddition(x); y != 32 {
t.Errorf("Mine is %v", y)
}
}
func TestFirst(t *testing.T) {
x := 10
a := Add{}
if y := a.First(x); y != 15 {
t.Errorf("First is %v", y)
}
}
输出:
$ go test -v
=== RUN TestBasicAddition
--- PASS: TestBasicAddition (0.00s)
=== RUN TestFirst
--- PASS: TestFirst (0.00s)
PASS
ok so/calculator 0.002s
$
- 1 回答
- 0 关注
- 225 浏览
添加回答
举报