Skip to Content
러닝 Go : Go 개발자처럼 생각하는 방법
book

러닝 Go : Go 개발자처럼 생각하는 방법

by 존 보드너, 윤대석
February 2022
Beginner to intermediate
452 pages
10h 48m
Korean
Hanbit Media, Inc.
Content preview from 러닝 Go : Go 개발자처럼 생각하는 방법
344
러닝 Go
WithContext
메서드를 사용해야하는 또 다른 상황이 있는데, 당신의 응용 프로그램에서 다른
HTTP
서비스로
HTTP
호출을 만들 때이다. 미들웨어를 통해 컨텍스트를 전달하는 경우와 같
WithContext
를 사용하여 외부로 나가는 요청에 컨텍스트를 설정할 수 있다.
type ServiceCaller struct {
client *http.Client
}
func (sc ServiceCaller) callAnotherService(ctx context.Context, data string)
(string, error) {
req, err := http.NewRequest(http.MethodGet,
“http://example.com?data=”+data, nil)
if err != nil {
return “”, err
}
req = req.WithContext(ctx)
resp, err := sc.client.Do(req)
if err != nil {
return “”, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return “”, fmt.Errorf(“Unexpected status code %d”,
resp.StatusCode)
}
// ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

효율적인 리눅스 명령어 사용의 기술

효율적인 리눅스 명령어 사용의 기술

Daniel J Barrett

Publisher Resources

ISBN: 9791162245309