Go ?? ?? ???? ?? ?? ?? ??
May 02, 2024 am 11:21 AMGo ?? ?? ????? ?? ??? ?? ? ?? ?? ??? ????. 1. ?? ?? ???? ? ???? ?? ??? ?? ??? ??? ?????. 2. ??? ??? ??? ???? ?? ??? ?????. , ?? ?? ??? ????? ? ?????. ?? ????? ?? ? ??? ???? ??? ?? ??? ?? 0? ????? ???.
Go ?? ?? ???? ?? ?? ?? ??
?? ???? ??? ???? ???? ???? ??? ?????. Go??? ?? ??? ?? ?? ??? ??? testing
???? ???? ?? ???? ??? ? ????. testing
包來執(zhí)行單元測試,其中包含處理錯誤的幾種策略。
錯誤處理策略
Go 中有兩種處理錯誤的主要策略:
1. 錯誤值
將錯誤表示為 error
Go?? ? ?? ?? ?? ?? ??? ????.
1. ?? ?
???error
??? ?? ??? ?????. ?? ????? ? ??? ????? ?? ?? ??? ???? ? ????. func TestMyFunction(t *testing.T) { err := myFunction() if err != nil { t.Errorf("myFunction returned an unexpected error: %v", err) } }
2. ?? ??
??????? ???? ??? ??? ??? ?????. ?? ??? ??? ??? ? ???? ?? ?? ??? ????? ? ?????. ??func TestMyConcurrentFunction(t *testing.T) { done := make(chan error) go func() { done <- myConcurrentFunction() }() select { case err := <-done: if err != nil { t.Errorf("myConcurrentFunction returned an unexpected error: %v", err) } case <-time.After(time.Second): t.Errorf("myConcurrentFunction did not complete within the timeout") } }?? ?? ? ???? ????? ??? ???? ?? ??? ?????. ??
func sum(numbers []int) int { total := 0 for _, num := range numbers { if num < 0 { return 0 } total += num } return total }?? ?? ? ??? ???? ?? ???? ??? ?????. ?? ??? ?? 0? ???? ??: ??
func TestSum(t *testing.T) { tests := []struct { input []int result int }{ {[]int{1, 2, 3}, 6}, {[]int{0, 0, 0}, 0}, {[]int{-1, 0, 1}, 0}, } for _, test := range tests { result := sum(test.input) if result != test.result { t.Errorf("sum(%v) returned %d, expected %d", test.input, result, test.result) } } }
? ??? Go ?? ?? ???? ?? ?? ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Golang ?? ????? ???? Gomega? ???? ?? Golang ?? ????? Gomega? ???? ??? ??? ?? ??? ? ??? ??? ??? ??? ???? ?? ?? ??? ??? ????????. Gomegagoget-ugithub.com/onsi/gomega ?? ???? Gomega ?? ??? ???? Gomega? ???? ? ?? ???? ????. 1. ?? ??? import "github.com/onsi/gomega" funcTest_MyFunction(t*testing.T){

Golang?? ?? ??? ???? ?? ??? ??? ??? ???? ??? ??? ??? ? ????. ?? ??? ?????? ?? ???? ???? ?? ??? ???? ??? ? ?? ??? ????? ? ??? ? ????. ??? ??? ????. error.Wrap ??? ???? ?? ??? ? ??? ?????. ? ???? ?? ??? ??? ??? ?????. fmt.Printf? ???? ??? ??? ???? ? ?? ????? ?? ???? ??? ? ????. ??? ??? ??? ??? ? ?? ??? ????? ??.Wrap ??? ?????.

???, ???, ?? ??? ? ??? ????? ??? ???? ???? ?? ??? ???? ???? ?? ?????. ???? ???? ?? ??, ??? ???? ??, ??? ?? ? ??? ??? ??? ?????. ?? ????? max() ??? ?? ??? ?? ??? ???? ?? ??? ????? ??? ???? ?????. ??? ??? ??? ??? ?? ??? ???? ???? ? ????.

PHPUnit? ???? ?? ?? ??? ??? ??? ???? ? ??? ? ?? ?? ?? PHP ?? ??? ????????. ???? PHPUnit? ???? ??? ??? ??? ??? ????? ??? ??? ?????. PHPUnit\Framework\TestCase? ???? ??? ???? ????. ???? ??? ???? ?? "test"? ???? ??? ??? ?????. ???? ???? ?? ??? ?? ??? ???? ?????. Vendor/bin/phpunit? ???? ???? ?? ?????? ???? ?????.

Go?? ?? ??? ????? ???? ??? ??? ????. error.Error()? ???? ?? ???? ? ????? ???? test.T.FatalError() ? test.T.Errorf()? ???? ?? ???? ???? ?????. ???? ??? ????? ?? ?????. require.NoError ? require.EqualError? ?? require ??? ??? ???? ?? ? ???? ?????.

?? ??: ? ??? ?? ??(?? ???)? ???? ??? ?? ?? ?? ??? ?? ?? ??? ???? ?? ??? ???? ???? ????? ???? ?? ??? ?? ??? ???? ???? ??

????? ??? ??? ?? ??? ???? ????? Golang?? HTTP ??? ???? ?? ?????. ??? ???? ???? ?? ??? ????. ??? ?? ??(?: ErrBadRequest)? ???? ???? HTTP ??? ?????. ??? ?? ?? ??? ????? ??? ?? ?? ??? ????. ?? ??? ???? ???? ? ??? ????? httputil.NewError ??? ?????.

Golang??? ?? ?? ??? ?? ?????. ??? ?? ???? error, ValueError, TypeError ? IndexOutOfBoundsError? ?????. ??? ?? ?? ??? ?? ?????? ???? ??? ? ????. ?? ?? ??? ?????. ?? ?? ??? ?? ?? ??? ? ????. ?? ??? ?????? ??? ???? ??? ???? ???? ??? Golang ????? ??? ? ????.
