调用中参数不足

Tec*_*Bar 0 go

我正在尝试创建一个显示用户 IP 的 go 应用程序。

我无法弄清楚我的日志控制台错误:

go:14: 调用 getJsonRes 时参数不足

去应用程序代码:

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, "", " ")
}
Run Code Online (Sandbox Code Playgroud)

Jak*_*yer 5

你的职能

func getJsonRes(r *http.Request)([]byte, error )

接受请求指针并返回字节数组和/或错误。

在这条线上

response, err := getJsonRes()

你可以不带参数地调用它。您可能打算执行以下操作

response, err := getJsonRes(r)