Reddit JSON을 사용하는 매우 기본적인 파서를 작성했으며 Go에서 오류를 구체적으로 관리 할 수 있는지 궁금합니다. 예를 들어Go에서 특정 오류를 지정하고 처리하는 방법은 무엇입니까?
내가 링크이 "가져 오기"방법이 있습니다
func Get(reddit string) ([]Item, error) {
url := fmt.Sprintf("http://reddit.com/r/%s.json", reddit)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, err
}
/*
* Other code here
*/
}
가 어떻게 말 처리 할 수 의 상태 코드에서 404 오류?
if resp.StatusCode == http.StatusNotfound {
//do stuff here
}
하지만 직접 if 문의 무리를 작성하지 않고 resp.StatusCode != http.StatusOK
을 관리 할 수있는 방법이 : 나는 404 오류 자체를 테스트 할 수 알아? switch 문에 err
을 사용할 수있는 방법이 있습니까?