github.com/nibnait/go-learn@v0.0.0-20220227013611-dfa47ea6d2da/src/test/chapter/ch3/20_err_test.go (about) 1 package ch3 2 3 import ( 4 "errors" 5 "testing" 6 ) 7 8 var LessThanTwoError = errors.New("n should be not less than 2") 9 var LargerThenHundredError = errors.New("n should be not larger than 100") 10 11 func GetFibonacci(n int) ([]int, error) { 12 if n < 2 { 13 return nil, LessThanTwoError 14 } 15 if n > 100 { 16 return nil, LargerThenHundredError 17 } 18 fibList := []int{1, 1} 19 20 for i := 2; i < n; i++ { 21 fibList = append(fibList, fibList[i-2]+fibList[i-1]) 22 } 23 return fibList, nil 24 } 25 26 func TestGetFibonacci(t *testing.T) { 27 if v, err := GetFibonacci(-1); err != nil { 28 switch err { 29 case LessThanTwoError: 30 t.Log("小于2") 31 default: 32 t.Log("未知错误") 33 } 34 t.Error(err) 35 } else { 36 t.Log(v) 37 } 38 }