Skip to content

Commit

Permalink
Explored dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
vedicpanda committed May 31, 2023
1 parent a87cdea commit 919c637
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions DependencyInjection/greet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
)

func Greet(writer io.Writer, name string) {
fmt.Fprintf(writer, "Hello, %s", name)
}

func MyGreeterHandler(w http.ResponseWriter, r *http.Request) {
Greet(w, "world")
}

func main() {
log.Fatal(http.ListenAndServe(":5001", http.HandlerFunc(MyGreeterHandler)))
}
18 changes: 18 additions & 0 deletions DependencyInjection/greet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"bytes"
"testing"
)

func TestGreet(t *testing.T) {
buffer := bytes.Buffer{}
Greet(&buffer, "Chris")

got := buffer.String()
want := "Hello, Chris"

if got != want {
t.Errorf("got %q want %q", got, want)
}
}

0 comments on commit 919c637

Please sign in to comment.