This is a creation in Article, where the information may have evolved or changed.
Urlshortpackage mainimport ( "bytes" "Encoding/json" " fmt" "io/ioutil" "Log" "Net/http " os") type apiresponse struct { Id, Kind, Longurl String}func main () { Longurl: = os. Args[len (OS. Args)-1] FMT. Printf ("begin:%s\n", Longurl) body: = bytes. Newbufferstring (FMT. Sprintf ( "{' Longurl ': '%s '}", Longurl)) request, Err: = http. Newrequest ( "POST", "Https://www.googleapis.com/urlshortener/v1/url", body) request. Header.add ("Content-type", "Application/json") client: = http. client{} Response, Err: = client. Do (Request) if err! = Nil { log. Fatal (err) }
Outputasbytes, err: = Ioutil. ReadAll (response. Body) Response. Body.close () var output apiresponse err = json. Unmarshal (Outputasbytes, &output) if err! = Nil { log. Fatal (Err) } fmt. Printf ("%s\n", Longurl)}