[Go] encoding/json 패키지 - json.Decoder, Unmarshal & io.ReadAll
·
Language/Go
회사 프로젝트에서 2가지 방법으로 http response를 읽고 decode 하고 있다는 걸 발견했다. 둘 다 제대로 서비스가 동작하고 있는데 내부 동작이 어떻게 다른지 차이점이 궁금해서 공부해보았다.json.Decoder, Decode, Unmarshal 그리고 io.ReadAll 을 중심으로 살펴볼 예정이다.🟡 코드 비교우선 코드부터 살펴보자.두 코드 모두 client.Do()를 이용해 response(*http.Response 타입)를 가져온다.차이점은 response body(io.ReadCloser 타입)를 읽고 원하는 go 변수에 값을 넣어주는 방법이다. if resp, err := client.Do(req); err != nil { return err} else if body, e..