Created
August 4, 2018 20:16
-
-
Save quii/5fb4d2fbd7eb96002e5149e18338d2b4 to your computer and use it in GitHub Desktop.
Dependencies
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 ( | |
"net/http" | |
"fmt" | |
) | |
type MessageGetter interface { | |
GetMessage() string | |
} | |
type Server struct { | |
aDependency MessageGetter | |
} | |
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, s.aDependency.GetMessage()) | |
} | |
type HardcodedDependency struct {} | |
func (h *HardcodedDependency) GetMessage() string { | |
return "Dependency injection is often overdone" | |
} | |
func main() { | |
myDependency := &HardcodedDependency{} | |
myServer := Server{aDependency:myDependency} | |
http.ListenAndServe(":5000", &myServer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment