Last active
December 28, 2017 22:02
-
-
Save rogerwelin/6a6f61ccebfb72d3e25fced5f188bae1 to your computer and use it in GitHub Desktop.
go-dependency-inject1
This file contains 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 ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
var ( | |
url string | |
) | |
func init() { | |
flag.StringVar(&url, "url", "http://google.com", "Which url to use") | |
flag.Parse() | |
} | |
type HttpClient interface { | |
Get(string) (*http.Response, error) | |
Post(string, string, io.Reader) (*http.Response, error) | |
} | |
func send(client HttpClient, link string) error { | |
response, err := client.Get(link) | |
if err != nil { | |
return err | |
} | |
if response == nil { | |
return err | |
} | |
body, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
return err | |
} | |
fmt.Println(string(body)) | |
return nil | |
} | |
func main() { | |
client := &http.Client{} | |
err := send(client, url) | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment