LoginSignup
0
1

More than 3 years have passed since last update.

MacでGo言語を使ってOpenWeatherMapAPIを使用した際の備忘録

Posted at

目的

MacでGo言語を使ってOpenWeatherMapAPIを使用した際の備忘録です

準備

環境
 macOS
 Homebrew

インストール

$ brew install go

テスト

hello world

本体コード

package main
import "fmt"

func main() {
    fmt.Printf("Hello World!\n")
}

実行

$ go run sample.go 
Hello World!

Setenv

本体コード

package main

import (
        "fmt"
        "os"
)

func main() {
        // 既存の環境変数を取得
        // fmt.Println("1. $HOME:",os.Getenv("HOME"))

        // プログラム中で設定してやることもできる
        fmt.Println("2. $TEST:",os.Getenv("TEST"))
        os.Setenv("TEST", "abcdedg")
        fmt.Println("3. $TEST:",os.Getenv("TEST"))

        // 環境変数を配列で取得する
        // fmt.Println(os.Environ())
}

実行

$ go run testenv.go
2. $TEST: 
3. $TEST: abcdedg

open weather map

以下のOpenWeatherMap APIラッパーを使わせて頂きます。

ramsgoli/Golang-OpenWeatherMap

Installation

mkdir $HOME/gocode
export GOPATH=$HOME/gocode
go get github.com/ramsgoli/Golang-OpenWeatherMap

export

export OWM_APP_ID="xxxxx" # your API Key

本体コード

sample.go
package main

import (
        "fmt"
        "os"
        "github.com/ramsgoli/Golang-OpenWeatherMap"
)

func main() {

        owm := openweathermap.OpenWeatherMap{API_KEY: os.Getenv("OWM_APP_ID")}

        var currentWeather *openweathermap.CurrentWeatherResponse
        //var err error

        //currentWeather, err = owm.CurrentWeatherFromCity("Osaka")
        currentWeather, _ = owm.CurrentWeatherFromCity("Osaka")

        fmt.Printf("The current temperature in %s is %f degrees\n", currentWeather.Name, currentWeather.Main.Temp)
}

実行

$ go run sample.go
The current temperature in Osaka is 79.650000 degrees

参考

GO言語をMacで使ってみる インストール
ramsgoli/Golang-OpenWeatherMap
Go言語で今日傘が必要か教えてくれる傘APIをつくってみた ~Mockテストもしっかりやるよ~
package openweathermap
動的言語だけやってた僕が、38日間Go言語を書いて学んだこと
Go言語でRESTful APIクライアント: Qiita APIを叩いてみよう!
Go開発環境をEmacsで整える
My take on creating a simple wrapper for the openweather API.

0
1
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
1