Created
August 26, 2016 17:50
-
-
Save manojkarthick/dd907891fa4b1e1fd9ba2d493c620b0a to your computer and use it in GitHub Desktop.
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" | |
| "os" | |
| ) | |
| //This is the JSON format we get from is.gd | |
| type AutoGenerated struct { | |
| Shorturl string `json:"shorturl"` | |
| } | |
| func main() { | |
| fmt.Println("is.gd API consumption") | |
| if len(os.Args) != 2 { | |
| fmt.Fprintf(os.Stderr, "Usage: %s URL\n", os.Args[0]) | |
| os.Exit(1) | |
| } | |
| baseURL := "https://is.gd/create.php?format=json&url=" | |
| URLToShorten := os.Args[1] | |
| getReqURL := baseURL + URLToShorten | |
| request, err := http.NewRequest("GET", getReqURL, nil) | |
| if err != nil { | |
| log.Fatal("NewRequest: ", err) | |
| return | |
| } | |
| client := &http.Client{} | |
| response, err := client.Do(request) | |
| if err != nil { | |
| log.Fatal("Do: ", err) | |
| return | |
| } | |
| defer response.Body.Close() | |
| var result AutoGenerated | |
| if err := json.NewDecoder(response.Body).Decode(&result); err != nil { | |
| log.Println(err) | |
| } | |
| fmt.Println("Shortened URL is: ", result.Shorturl) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment