Skip to content

Instantly share code, notes, and snippets.

@wilkice
Created September 13, 2021 08:08
Show Gist options
  • Select an option

  • Save wilkice/d8c8469b98ea9953b18f7765e07e571c to your computer and use it in GitHub Desktop.

Select an option

Save wilkice/d8c8469b98ea9953b18f7765e07e571c to your computer and use it in GitHub Desktop.
[go decode to nested json] #go
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
func main() {
resp, err := http.Get("https://httpbin.org/get")
if err != nil {
log.Fatalln(err)
}
var result struct {
Key struct {
Host string
UserAgent string `json:"User-Agent"`
} `json:"headers"`
Url string
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%v \n", result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment