google 短网址服务 -- Go 版
// urlshort package main import ( "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) }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。