Skip to content

Instantly share code, notes, and snippets.

@janithl
Created September 3, 2018 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janithl/d3513971b168e4569d0845433f16633c to your computer and use it in GitHub Desktop.
Save janithl/d3513971b168e4569d0845433f16633c to your computer and use it in GitHub Desktop.
Simple client that HTTP gets and JSON parses an endpoint
package main
import "net/http"
import "fmt"
import "io/ioutil"
import "encoding/json"
// Artist is a struct to hold artist values
type Artist struct {
Name, Shortname, Reknown, Bio string
}
func main() {
resp, err := http.Get("https://gist.githubusercontent.com/planetoftheweb/98f35786733c8cccf81e/raw/f3dad774ed1fe20b36011b1261bb392ee759b867/data.json")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
artists := make([]Artist, 1)
if err := json.Unmarshal(body, &artists); err != nil {
panic(err)
}
for _, artist := range artists {
fmt.Printf("%s (%s)\n%s\n\n", artist.Name, artist.Shortname, artist.Reknown)
fmt.Println(artist.Bio + "\n\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment