#Go言語 ## ポイント - [[http.NewRequest]]を使ってリクエストを作成する - [[http.Request.Header.Set]]を使ってヘッダをセットする - [[http.Client]]を作成して[[http.Client.Do]]でリクエストする ```go type IClient interface { GetHoge(authorization string) (*response.Root, error) } type Client struct { BaseUrl string } func NewClient(baseUrl string) IClient { return Client{ BaseUrl: baseUrl, } } func (c Client) GetHoge(authorization string) (*response.Root, error) { uri := c.createUri("/hoge", url.Values{}) req, err := http.NewRequest("GET", uri, nil) if err != nil { // TODO: 例外処理 } req.Header.Set("Authorization", authorization) client := new(http.Client) res, err := client.Do(req) if err != nil { // TODO: 例外処理 } if res.StatusCode >= 500 { // TODO: 例外処理 } if res.StatusCode >= 400 { // TODO: 例外処理 } var r response.Root if err := json.NewDecoder(res.Body).Decode(&r); err != nil { // TODO: 例外処理 } defer res.Body.Close() return &r, nil } func (c Client) createUri(path string, query url.Values) string { return fmt.Sprintf("%s%s?%s", c.BaseUrl, path, query.Encode()) } ``` ---- **💽Change log** - #2022/01/26 作成