Created
September 13, 2021 08:08
-
-
Save wilkice/d8c8469b98ea9953b18f7765e07e571c to your computer and use it in GitHub Desktop.
[go decode to nested json] #go
This file contains hidden or 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 ( | |
| "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