개발/go

[golang] Covid19 정보 조회

xwing 2021. 1. 27. 22:38

Covid19 

1년넘게 지겹게 우릴를 괴롭히고 있는는 #Covid19에 대한 정보를 얻을 수 있는 공개된 API는 이미 많이 있다..

구글검색 조금만 하면 쉽게 찾을 수 있다.

이번엔 API를 이용해서 우리나라의 코로나19 감염발생자수를 조회해 보자


package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type Country struct {
	Country     string
	CountryCode string
	Province    string
	City        string
	CityCode    string
	Lat         string
	Lon         string
	Cases       int
	Status      string
	Date        string
}

func main() {

	url := "https://api.covid19api.com/dayone/country/korea-south/status/confirmed"
	method := "GET"

	client := &http.Client{}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
	}
	res, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)

	var data []Country
	err1 := json.Unmarshal(body, &data)

	if err1 != nil {
		panic(err)
	}

	for _, c := range data {
		fmt.Println("[", c.Country, "]", c.Date, ":", c.Cases)
	}
}

 


위 소스중에서 country code만 변경해주면 다른나라의 추이도 조회해 볼 수 있다. country code 역시 해당 사이트에서 쉽게 찾을 수 있다.

결과는 이렇다..

이제 지겨운 #covid19 좀 끝내자!!!

 

'개발 > go' 카테고리의 다른 글

[golnag] 기초 excelize  (0) 2021.01.18
[GOLANG] 날씨조회 (openweather)  (0) 2017.03.25
Go language  (0) 2017.03.24