Reputation: 27
I'm trying to create a go app that will display the users IP.
I can't figure out my Log Console error:
go:14: not enough arguments in call to getJsonRes
Go app code:
package main
import (
"encoding/json"
"net/http"
"fmt"
)
type Addrs struct {
ip string
}
func handler(w http.ResponseWriter, r *http.Request) {
response, err := getJsonRes()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, string(response))
}
func main() {
http.HandleFunc("/", handler)
}
func getJsonRes(r *http.Request)([]byte, error ) {
ip := Addrs{ r.RemoteAddr }
return json.MarshalIndent(ip, "", " ")
}
Upvotes: 0
Views: 14204
Reputation: 34708
Your function
func getJsonRes(r *http.Request)([]byte, error )
Takes a request pointer and returns a byte array and or an error.
On this line
response, err := getJsonRes()
You call it with with no arguments. You probably meant to do the following
response, err := getJsonRes(r)
Upvotes: 5