Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment 1
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aksel Baardsen
Assignment 1
Commits
f8134408
Commit
f8134408
authored
5 years ago
by
Aksel Baardsen
Browse files
Options
Downloads
Patches
Plain Diff
testing countries api and json parsing, not working
parent
6473a20a
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
main.go
+21
-1
21 additions, 1 deletion
main.go
models/countries.go
+32
-6
32 additions, 6 deletions
models/countries.go
models/species.go
+1
-1
1 addition, 1 deletion
models/species.go
with
54 additions
and
8 deletions
main.go
+
21
−
1
View file @
f8134408
package
main
import
(
"assignment-1/models"
"encoding/json"
"fmt"
"log"
"net/http"
...
...
@@ -34,7 +36,25 @@ func dHandler(w http.ResponseWriter, r *http.Request) {
}
func
cHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
_
,
_
=
fmt
.
Fprintln
(
w
,
"aiaiai"
)
vars
:=
mux
.
Vars
(
r
)
_
,
_
=
fmt
.
Fprintln
(
w
,
vars
[
"country_identifier"
])
resp
,
_
:=
http
.
Get
(
"http://restcountries.eu/rest/v2/alpha/"
+
vars
[
"country_identifier"
]
+
"/"
)
cunt1
:=
&
models
.
Country
{}
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
cunt1
)
if
err
!=
nil
{
fmt
.
Print
(
"on it"
)
}
fmt
.
Fprintln
(
w
,
cunt1
.
CountryName
)
qq
,
_
:=
json
.
Marshal
(
resp
.
Body
)
fmt
.
Fprintln
(
w
,
string
(
qq
))
//country := models.GetCountryByCode(vars["country_identifier"])
//_, _ = fmt.Fprintln(w, country)
}
...
...
This diff is collapsed.
Click to expand it.
models/countries.go
+
32
−
6
View file @
f8134408
package
models
type
country
struct
{
const
codeAPI
=
"https://restcountries.eu/rest/v2/alpha/"
type
Country
struct
{
Code
string
`json:"code"`
CountryName
string
`json:"countryname"`
CountryFlag
string
`json:"countryflag"`
Species
[]
s
pecie
`json:"species"`
//
Species
[]
S
pecie
`json:"species"`
SpeciesKey
[]
int
`json:"specieskey"`
}
/*
func GetCountryByCode(code string) Country {
if err != nil {
//handle
}
c := &Country{}
err = json.NewDecoder(resp.Body).Decode(c)
if err != nil {
//handle
}
defer resp.Body.Close()
return *c
}*/
func
(
c
*
Country
)
show
()
{
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
models/species.go
+
1
−
1
View file @
f8134408
package
models
type
s
pecie
struct
{
type
S
pecie
struct
{
Key
int
`json:"key"`
Kingdom
string
`json:"kingdom"`
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment