Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Created August 28, 2016 22:02
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 juanpablocs/1c8a3549620e0d461e3385bd952eb92f to your computer and use it in GitHub Desktop.
Save juanpablocs/1c8a3549620e0d461e3385bd952eb92f to your computer and use it in GitHub Desktop.
demo request and process json soundcloud with golang
package main
import (
"fmt"
"log"
"net/http"
"encoding/json"
"io/ioutil"
)
const API_KEY = "38682c40bf52f8885ae2ba6dc6ffae81"
const QUERY = "jpmaster"
const LIMIT int = 2
func urlify(path string) string {
return fmt.Sprintf(path, API_KEY, QUERY, LIMIT)
}
// represents a search result
type SearchResult struct {
Id int
Title string
Created_at string
Duration int
Stream_url string
Description string
Permalink_url string
Download_url string
CreatedAtFormated string
Downloadable bool
}
var SearchResults []*SearchResult
func main() {
// http request
res,err := http.Get(urlify("http://api.soundcloud.com/tracks?client_id=%s&filter=downloadable&q=%s&limit=%d"))
resbody, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
res.Body.Close()
err = json.Unmarshal(resbody, &SearchResults)
if err != nil {
log.Fatal(err)
return
}
for _, v := range SearchResults {
fmt.Printf("%s - %d\n", v.Title, v.Id)
}
}
//command
//go run soundcloud.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment