Skip to content

Instantly share code, notes, and snippets.

@quii
Created August 4, 2018 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quii/5fb4d2fbd7eb96002e5149e18338d2b4 to your computer and use it in GitHub Desktop.
Save quii/5fb4d2fbd7eb96002e5149e18338d2b4 to your computer and use it in GitHub Desktop.
Dependencies
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