Go 调用中没有足够的参数
我正在尝试创建一个可以显示用户IP的go应用程序 我无法找出我的日志控制台错误: go:14:调用getJsonRes时参数不足 Go应用程序代码:Go 调用中没有足够的参数,go,Go,我正在尝试创建一个可以显示用户IP的go应用程序 我无法找出我的日志控制台错误: go:14:调用getJsonRes时参数不足 Go应用程序代码: package main import ( "encoding/json" "net/http" "fmt" ) type Addrs struct { ip string } func handler(w http.ResponseWriter, r *http.Request) { response
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, "", " ")
}
你的职能
func getJsonRes(r*http.Request)([]字节,错误)
获取请求指针并返回字节数组和/或错误
在这条线上
响应,错误:=getJsonRes()
你称它为没有参数。你可能打算做以下事情
响应,err:=getJsonRes(r)
您的功能
func getJsonRes(r*http.Request)([]字节,错误)
获取请求指针并返回字节数组和/或错误
在这条线上
响应,错误:=getJsonRes()
你称它为没有参数。你可能打算做以下事情
响应,err:=getJsonRes(r)