You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Greet(writer io.Writer, name string) {
|
|
|
|
fmt.Fprintf(writer, "Hello, %s\n", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// func main() {
|
|
|
|
// Greet(os.Stdout, "Drew")
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Here you can see that since now our greeter function takes an object that
|
|
|
|
// implements the io.Writer interface we can now pass an object like the http.ResponseWriter
|
|
|
|
// to it. DI let's us reuse our code in a new context.
|
|
|
|
func MyGreeterHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
Greet(w, "world")
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.Fatal(http.ListenAndServe(":5001", http.HandlerFunc(MyGreeterHandler)))
|
|
|
|
}
|