Created
August 28, 2016 22:02
-
-
Save juanpablocs/1c8a3549620e0d461e3385bd952eb92f to your computer and use it in GitHub Desktop.
demo request and process json soundcloud with golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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