Golang - library / package that returns json string from http request

Is there a library / package that returns a json string received in response to an HTTP request. It's pretty simple, so I can write my own, but would prefer the existing / tested code to reinvent the wheel.

I currently have:

func getJsonStr(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil { return []byte{0}, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return []byte{0}, err } return body, nil } 

EDIT: I'm looking for something like a Node module 'request' that allows me to do this on one line, for example: jsonStr, err: = getJsonStr (url).

+4
source share
2 answers

Since you did not want to define the structure, it is also possible (but a little ugly) to untie a map of a card like [string] interface {} {}

 package main import ( "encoding/json" "fmt" "io/ioutil" "net/http" ) func main() { data := map[string]interface{}{} r, _ := http.Get("http://api.stackoverflow.com/1.1/tags?pagesize=100&page=1") defer r.Body.Close() body, _ := ioutil.ReadAll(r.Body) json.Unmarshal(body, &data) fmt.Println("Total:", data["total"], "page:", data["page"], "pagesize:", data["pagesize"]) // Total: 34055 page: 1 pagesize: 100 } 
+11
source

There is nothing wrong with using an existing system package and it is quite simple to get json data over http in go

 package main import ( "encoding/json" "fmt" "net/http" ) func main() { var data struct { Items []struct { Name string Count int Is_required bool Is_moderator_only bool Has_synonyms bool } } r, _ := http.Get("https://api.stackexchange.com/2.2/tags?page=1&pagesize=100&order=desc&sort=popular&site=stackoverflow") defer r.Body.Close() dec := json.NewDecoder(r.Body) dec.Decode(&data) for _, item := range data.Items { fmt.Printf("%s = %d\n", item.Name, item.Count) } } 

Exit go run main.go

 java = 781454 javascript = 769128 c# = 744294 php = 692360 android = 617892 jquery = 570461 python = 379608 html = 374617 c++ = 341533 ios = 300992 mysql = 296223 css = 276063 sql = 258178 asp.net = 244833 objective-c = 215095 .net = 202087 iphone = 199749 ruby-on-rails = 190497 c = 166226 ruby = 123840 sql-server = 119341 arrays = 116975 ajax = 109786 regex = 107215 xml = 106705 asp.net-mvc = 101921 json = 101545 wpf = 96016 linux = 92266 django = 88008 database = 86638 eclipse = 79952 vb.net = 78888 r = 78401 xcode = 76326 windows = 74359 angularjs = 73266 string = 71149 html5 = 68094 node.js = 66731 wordpress = 65125 multithreading = 64168 facebook = 62460 excel = 58402 spring = 57380 image = 56798 winforms = 55268 forms = 54263 ruby-on-rails-3 = 52611 osx = 51292 oracle = 50876 git = 50019 performance = 48867 swing = 48768 algorithm = 48258 apache = 47355 bash = 46347 linq = 45169 visual-studio-2010 = 43907 entity-framework = 43776 perl = 42740 web-services = 42609 matlab = 42112 hibernate = 41410 visual-studio = 40307 wcf = 39948 sql-server-2008 = 39394 mongodb = 38632 asp.net-mvc-3 = 38018 list = 37914 qt = 37505 .htaccess = 37305 css3 = 36505 vba = 36368 sqlite = 36274 actionscript-3 = 35594 file = 35171 twitter-bootstrap = 34911 postgresql = 34757 function = 34747 codeigniter = 33439 api = 32964 class = 32888 scala = 32763 shell = 32407 google-maps = 31961 cocoa = 31744 ipad = 31600 jsp = 31510 cocoa-touch = 30994 tsql = 30976 sockets = 30635 flash = 30555 jquery-ui = 30506 asp.net-mvc-4 = 30278 validation = 30166 security = 30035 delphi = 29758 unit-testing = 29717 rest = 29475 
+8
source

Source: https://habr.com/ru/post/1494780/


All Articles