16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GolangでAPIを叩く

Posted at

初めてのGolang

GolangでAPIサーバを書こうと思い手始めにライブドアのWeatherHacksのAPIを叩いてみました。

Jsonパースはいろいろ大変そうなので、今回はデータ取得まで。

コード

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

func main() {
	values := url.Values{}
	values.Add("city", "400040")
	resp, err := http.Get("http://weather.livedoor.com/forecast/webservice/json/v1" + "?" + values.Encode())

	if err != nil {
		fmt.Println(err)
		return
	}

	defer resp.Body.Close()

	execute(resp)
}

func execute(response *http.Response) {
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(string(body))
}

結果

{"pinpointLocations":[{"link":"http://weather.livedoor.com/area/forecast/4020200","name":"\u5927\u725f\u7530\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4020300","name":"\u4e45\u7559\u7c73\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4020700","name":"\u67f3\u5ddd\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4021000","name":"\u516b\u5973\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4021100","name":"\u7b51\u5f8c\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4021200","name":"\u5927\u5ddd\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4021600","name":"\u5c0f\u90e1\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4022500","name":"\u3046\u304d\u306f\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4022800","name":"\u671d\u5009\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4022900","name":"\u307f\u3084\u307e\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/4044700","name":"\u7b51\u524d\u753a"},{"link":"http://weather.livedoor.com/area/forecast/4044800","name":"\u6771\u5cf0\u6751"},{"link":"http://weather.livedoor.com/area/forecast/4050300","name":"\u5927\u5200\u6d17\u753a"},{"link":"http://weather.livedoor.com/area/forecast/4052200","name":"\u5927\u6728\u753a"},{"link":"http://weather.livedoor.com/area/forecast/4054400","name":"\u5e83\u5ddd\u753a"}]

以下略
16
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?