Created
March 21, 2018 21:06
-
-
Save adrianuf22/ebd86af8f80d168aaa4cd33d13e7cd0a to your computer and use it in GitHub Desktop.
pseudo_strategy.go
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 ( | |
"fmt" | |
"net/http" | |
) | |
func main() { | |
fakeArg := "fake" | |
switch fakeArg { | |
case "fake": | |
callService(&FakeServiceClient{}) | |
case "http": | |
callService(&HttpServiceClient{}) | |
default: | |
fmt.Println("Service not defined") | |
return | |
} | |
} | |
func callService(serviceClient ServiceClient) { | |
_, err := TestMyClient(serviceClient) | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
if err == nil { | |
fmt.Println("Success!") | |
} | |
} | |
type ServiceClient interface { | |
Get(req *http.Request) (*http.Response, error) | |
} | |
func TestMyClient(client ServiceClient) (response *http.Response, err error) { | |
request, err := http.NewRequest("GET", "https://www.reallycoolurl.com", nil) | |
if (err != nil) { | |
return | |
} | |
response, err = client.Get(request) | |
if err != nil { | |
return | |
} | |
return | |
} | |
type HttpServiceClient struct { | |
} | |
func (c *HttpServiceClient) Get(req *http.Request) (*http.Response, error) { | |
return http.Get("https://www.google.com") | |
} | |
type FakeServiceClient struct { | |
} | |
func (c *FakeServiceClient) Get(req *http.Request) (*http.Response, error) { | |
return &http.Response{}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment