package common import ( "encoding/json" "fmt" "io/ioutil" "net/http" "time" ) type Status struct { TimeStamp time.Time `json:"time_stamp,omitempty"` ErrorCode int `json:"error_code,omitempty"` ErrorMessage string `json:"error_message,omitempty"` Elapsed int `json:"elapsed,omitempty"` CreditCount int `json:"credit_count,omitempty"` Notice int `json:"notice,omitempty"` } type Data struct { ID int `json:"id,omitempty"` Symbol string `json:"symbol,omitempty"` Name string `json:"name,omitempty"` Amount int `json:"amount,omitempty"` LastUpdated time.Time `json:"last_updated,omitempty"` Quote Quote `json:"quote,omitempty"` } type DataList struct { Data [1]Data } type Quote struct { USD PriceSet `json:"usd,omitempty"` } type PriceSet struct { Price float32 LastUpdated time.Time } type retSet struct { Status Status `json:"status,omitempty"` Data [10]Data `json:"data,omitempty"` } func sendPacket(uri string) retSet { var _r retSet const headerKey = "X-CMC_PRO_API_KEY" const apiKey = "4ef32866-4674-412b-9f7f-743c5a75d05c" req, err := http.NewRequest("GET", uri, nil) if err != nil { panic(err) } req.Header.Add(headerKey, apiKey) _client := &http.Client{} resp, err := _client.Do(req) if err != nil { panic(err) } bytes, _ := ioutil.ReadAll(resp.Body) str := string(bytes) //바이트를 문자열로 pb := []byte(str) fmt.Println(str) if err := json.Unmarshal(pb, &_r); err != nil { panic(err) } fmt.Printf("data %+v", _r) defer resp.Body.Close() return _r } func ObtainCurrencyPrice() (float64, float64, float64) { var uri = "https://pro-api.coinmarketcap.com/v2/tools/price-conversion" mf1_uri := fmt.Sprintf("%s?amount=1&symbol=MF1", uri) ret := sendPacket(mf1_uri) // mf1 = fmt.Sprintf("%f", ret.Data[0].Quote.USD.Price) mf1 := float64(ret.Data[0].Quote.USD.Price) mf2_uri := fmt.Sprintf("%s?amount=1&symbol=MF2", uri) ret = sendPacket(mf2_uri) // mr, _ := strconv.ParseFloat(fmt.Sprintf("%f", ret.Data[0].Quote.USD.Price), 64) mr := float64(ret.Data[0].Quote.USD.Price) eth_uri := fmt.Sprintf("%s?amount=1&symbol=ETH", uri) ret = sendPacket(eth_uri) // eth, _ := strconv.ParseFloat(fmt.Sprintf("%f", ret.Data[0].Quote.USD.Price), 64) eth := float64(ret.Data[0].Quote.USD.Price) return eth, mf1, mr }