我正在嘗試創建一個顯示用戶 IP 的 go 應用程序。我無法弄清楚我的日志控制臺錯誤:go:14: 沒有足夠的參數調用 getJsonRes去應用代碼:package mainimport ( "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, "", " ")}
1 回答

藍山帝景
TA貢獻1843條經驗 獲得超7個贊
你的職能
func getJsonRes(r *http.Request)([]byte, error )
接受一個請求指針并返回一個字節數組和/或一個錯誤。
在這條線上
response, err := getJsonRes()
你不帶參數調用它。您可能打算執行以下操作
response, err := getJsonRes(r)
- 1 回答
- 0 關注
- 195 瀏覽
添加回答
舉報
0/150
提交
取消