Dependency Injection
Where two types have the same interface, you can provide the interface as a type, and then pass in either of those types.
Example
io.Writer is an interface
package dependency_injection
import (
"fmt"
"io"
)
func Greet(buffer io.Writer, name string) {
fmt.Fprintf(buffer, "hello, %v", name)
}
Then implement it
package main
import (
"log"
"net/http"
"example.com/hello/dependency_injection"
)
func MyGreeterHandler(w http.ResponseWriter, r *http.Request) {
dependency_injection.Greet(w, "world")
}
func main() {
log.Fatal(http.ListenAndServe(":5001", http.HandlerFunc(MyGreeterHandler)))
}