Skip to content

Instantly share code, notes, and snippets.

@rogerwelin
Last active December 28, 2017 22:02
Show Gist options
  • Save rogerwelin/6a6f61ccebfb72d3e25fced5f188bae1 to your computer and use it in GitHub Desktop.
Save rogerwelin/6a6f61ccebfb72d3e25fced5f188bae1 to your computer and use it in GitHub Desktop.
go-dependency-inject1
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