Commit ccd22151 authored by Aksel Baardsen's avatar Aksel Baardsen
Browse files

Now trying json.Unmarshal for decoding

parent 59b7af59
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="GoUnhandledErrorResult" enabled="true" level="WARNING" enabled_by_default="true">
<methods>
<method importPath="hash" receiver="Hash" name="Write" />
<method importPath="strings" receiver="*Builder" name="Write" />
<method importPath="strings" receiver="*Builder" name="WriteByte" />
<method importPath="bytes" receiver="*Buffer" name="WriteRune" />
<method importPath="bytes" receiver="*Buffer" name="Write" />
<method importPath="bytes" receiver="*Buffer" name="WriteString" />
<method importPath="strings" receiver="*Builder" name="WriteString" />
<method importPath="bytes" receiver="*Buffer" name="WriteByte" />
<method importPath="strings" receiver="*Builder" name="WriteRune" />
<method importPath="math/rand" receiver="*Rand" name="Read" />
<method importPath="io" receiver="ReadCloser" name="Close" />
</methods>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
......@@ -2,6 +2,8 @@ package models
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
......@@ -18,19 +20,38 @@ type Country struct {
}
func GetCountryByCode(code string) Country {
var x Country
url := countryApi + code + "/"
resp, err:= http.Get(countryApi + code + "/")
resp, err:= http.Get(url)
if err != nil {
// handle pls
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
err = json.Unmarshal([]byte(bodyString), &x)
if err != nil {
log.Fatal(err)
}
//log.Info(bodyString)
}
/*
country := &Country{}
err = json.NewDecoder(resp.Body).Decode(country)
if err != nil {
// handle pls
}
defer resp.Body.Close()
log.Fatal(err)
}*/
return *country
return x
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment