如果我理解正確的話,您需要在某些條件下檢查正確的錯誤返回。 Here is: func coolFunction(input int) (int, error) { var err error var number int if input == 1 { err = errors.New("This is an error") number = 400 } else { err = nil number = 200 } return number, err} 然后在測試文件中,您需要對預期錯誤的事實(正確地)有一個理解或協議。就像expectError bool下面的一個標志,對于您的情況也是如此。 您還可以為特定的錯誤類型設置字段,并檢查返回的類型是否正是那個類型。 func TestCoolFunction(t *testing.T) { var testTable = []struct { desc string in int expectError bool out int }{ {"Should_fail", 1, true, 0}, {"Should_pass", 100, false, 200}, } for _, tt := range testTable { t.Run(tt.desc, func(t *testing.T) { out, err := coolF