Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Johannes Tangen
Assignment1
Commits
62390f79
Commit
62390f79
authored
5 years ago
by
jotangen
Browse files
Options
Downloads
Patches
Plain Diff
Creating get and parser
parent
9463ff7e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
main.go
+35
-0
35 additions, 0 deletions
main.go
main_test.go
+9
-0
9 additions, 0 deletions
main_test.go
webapp.go
+47
-0
47 additions, 0 deletions
webapp.go
with
91 additions
and
0 deletions
main.go
0 → 100644
+
35
−
0
View file @
62390f79
package
main
import
(
"fmt"
"html"
"log"
"net/http"
"os"
)
//Server that prints halli
func
helloHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprintf
(
w
,
"Hello, %q"
,
html
.
EscapeString
(
r
.
URL
.
Path
))
}
func
main
()
{
port
:=
os
.
Getenv
(
"PORT"
)
port
=
"8080"
if
port
==
""
{
log
.
Fatal
(
"$PORT must be set"
)
}
//This is code for tsting receiving get req
//From YT:Simple GET req example
//Get country by name
country
:=
getCountryName
()
fmt
.
Println
(
"Country name is s%"
,
country
)
http
.
HandleFunc
(
"/"
,
helloHandler
)
log
.
Fatal
(
http
.
ListenAndServe
(
":"
+
port
,
nil
))
}
This diff is collapsed.
Click to expand it.
main_test.go
0 → 100644
+
9
−
0
View file @
62390f79
package
main
/*func TestMain(t *testing.T) {
want := "Hello, world."
if got := Hello(); got != want {
t.Errorf("Hello() = %q, want %q", got, want)
}
}
*/
This diff is collapsed.
Click to expand it.
webapp.go
0 → 100644
+
47
−
0
View file @
62390f79
package
main
import
(
"encoding/json"
"fmt"
"net/http"
)
const
apiRoot
=
"https://restcountries.eu/rest/v2/alpha/"
//We receive JSON code and we parse it
//CntrInfo contains a response for restcountries endpoint
type
CntrInfo
struct
{
CallingCode
int32
//"47"
Name
string
`json:"name"`
//"country":"norway"
NameCode
string
//"NO"
}
//Should return the name Norway
func
getCountryName
()
string
{
//Change hardcoded to func parameters "We pass them"
countryCode
:=
"NO"
url
:=
apiRoot
+
countryCode
//resp is json, so we need to parse it
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
fmt
.
Println
(
"Error:we have a problem"
,
err
)
return
""
}
//Closes when surrounded functions are finished
defer
resp
.
Body
.
Close
()
//Pointer to struct
ci
:=
&
CntrInfo
{}
//resp.Body JSON
//NewDecoder returns a new decoder that reads from r
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
ci
)
return
"ci.Name"
}
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