Skip to main content
Remove semicolons and fix example
Source Link
sineemore
  • 1.8k
  • 12
  • 33
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
    handlePost(w, r);
})

can be simplified to

r.Post("/", handlePost(w, r));

since both functions have same signature.


fmt.Fprintf(w, "dummy response will go here");

No need to use printf family functions where you don't need formatting. You can write directly with

w.Write([]byte("dummy response will go here"))

or with

io.WriteString(w, "dummy response will go here")

responsebody, _ := ioutil.ReadAll(resp.Body)
fmt.Println("backend response Body:", string(responsebody))

This one reads the whole response into memory for no reason. Use io.Copy to write it more efficiently:

fmt.Print("backend response Body: ")
_, err := io.Copy(os.Stdout, resp.Body)

I'm not worried about error handling yet.

Then everything else is totally fine.

r.Post("/", func(w http.ResponseWriter, r *http.Request) {
    handlePost(w, r);
})

can be simplified to

r.Post("/", handlePost(w, r));

since both functions have same signature.


fmt.Fprintf(w, "dummy response will go here");

No need to use printf family functions where you don't need formatting. You can write directly with

w.Write([]byte("dummy response will go here"))

or with

io.WriteString(w, "dummy response will go here")

responsebody, _ := ioutil.ReadAll(resp.Body)
fmt.Println("backend response Body:", string(responsebody))

This one reads the whole response into memory for no reason. Use io.Copy to write it more efficiently:

fmt.Print("backend response Body: ")
_, err := io.Copy(os.Stdout, resp.Body)

I'm not worried about error handling yet.

Then everything else is totally fine.

r.Post("/", func(w http.ResponseWriter, r *http.Request) {
    handlePost(w, r)
})

can be simplified to

r.Post("/", handlePost)

since both functions have same signature.


fmt.Fprintf(w, "dummy response will go here")

No need to use printf family functions where you don't need formatting. You can write directly with

w.Write([]byte("dummy response will go here"))

or with

io.WriteString(w, "dummy response will go here")

responsebody, _ := ioutil.ReadAll(resp.Body)
fmt.Println("backend response Body:", string(responsebody))

This one reads the whole response into memory for no reason. Use io.Copy to write it more efficiently:

fmt.Print("backend response Body: ")
_, err := io.Copy(os.Stdout, resp.Body)

I'm not worried about error handling yet.

Then everything else is totally fine.

Source Link
sineemore
  • 1.8k
  • 12
  • 33

r.Post("/", func(w http.ResponseWriter, r *http.Request) {
    handlePost(w, r);
})

can be simplified to

r.Post("/", handlePost(w, r));

since both functions have same signature.


fmt.Fprintf(w, "dummy response will go here");

No need to use printf family functions where you don't need formatting. You can write directly with

w.Write([]byte("dummy response will go here"))

or with

io.WriteString(w, "dummy response will go here")

responsebody, _ := ioutil.ReadAll(resp.Body)
fmt.Println("backend response Body:", string(responsebody))

This one reads the whole response into memory for no reason. Use io.Copy to write it more efficiently:

fmt.Print("backend response Body: ")
_, err := io.Copy(os.Stdout, resp.Body)

I'm not worried about error handling yet.

Then everything else is totally fine.