LoginSignup
0
0

More than 1 year has passed since last update.

Go: Yahoo 郵便番号検索API の使い方

Posted at

参考ページ
郵便番号検索API

get_address.go
// ---------------------------------------------------------------
//
//	get_address.go
//
//					Jul/31/2022
// ---------------------------------------------------------------
package main

import (
	"os"
	"fmt"
	"io/ioutil"
	"net/http"
	"encoding/json"
	"github.com/joho/godotenv"
)

// ---------------------------------------------------------------
func json_parse_proc(str_json string) {
	var jsonBlob = []byte(str_json)
	var jsonObj interface{}
	_ = json.Unmarshal(jsonBlob, &jsonObj)

	address := jsonObj.(map[string]interface{})["Feature"].([]interface{})[0].(map[string]interface{})["Property"].(map[string]interface{})["Address"]

	fmt.Println(address)
}

// ---------------------------------------------------------------
func main() {
	fmt.Fprintf (os.Stderr,"*** 開始 ***\n")

	err := godotenv.Load(".env")
	if err != nil {
		panic(err)
		}

	appid := os.Getenv("APPID")

	code_zip := os.Args[1]
	fmt.Fprintf (os.Stderr,"%s\n",code_zip)
	url_base := "https://map.yahooapis.jp/search/zip/V1/zipCodeSearch"
	url_target := url_base + "?query=" + code_zip + "&output=json"
	req, _ := http.NewRequest("GET", url_target, nil)
	req.Header.Set("USER-AGENT", "Yahoo AppID: " + appid)
	client := new(http.Client)
	res, err := client.Do(req)
	if err != nil {
		fmt.Println("Request error:", err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println("Request error:", err)
		return
	}

	str_json := string(body)

	json_parse_proc(str_json)

	fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------
.env
APPID = 'dj0zaiZpPW9NN3F****'

実行の準備

go mod init member
go mod tidy

実行例

$ go run get_address.go 131-8634
*** 開始 ***
131-8634
東京都墨田区押上1-1-2東京スカイツリーイーストタワー11F
*** 終了 ***

次のバージョンで確認しました。

$ go version
go version go1.18.4 linux/amd64
0
0
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
0
0