This is a creation in Article, where the information may have evolved or changed.
File Mymath.go
Package Mymathfunc Add (A, b int) int {return a + B}func Max (A, B int) (ret int) {ret = AIF B > A {ret = B}return}
Test file Mymath_test.go (all test files must end with *_test.go)
Package Mymath_testimport ("MyMath" "testing") type mathtest struct {A, B, ret int}var addtest = []mathtest{mathtest{4, 6, 1 0},mathtest{5, 6, 11},mathtest{2,-6, -4},}var maxtest = []mathtest{mathtest{3, 5, 5},mathtest{-3, 5, 5},mathTest{-3,-5, -3},}func Testadd (t *testing. T) {For _, V: = Range Addtest {ret: = MyMath. ADD (V.A, V.B) if ret! = V.ret {T.errorf ("%d add%d, want%d, but get%d", V.A, V.B, V.ret, ret)}}}func Testmax (t *testing.t ) {for _, V: = Range Maxtest {ret: = MyMath. Max (V.A, V.B) if ret! = V.ret {T.errorf ("the max number between%d and%d is want%d, but get%d", V.A, V.B, V.ret, ret)}}
Execute Go Test
PASS OK myweb/lib/session 3.863s