gpt4 book ai didi

go - 调用中没有足够的参数

转载 作者:IT王子 更新时间:2023-10-29 02:34:04 25 4
gpt4 key购买 nike

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

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

go:14: not enough arguments in call to getJsonRes

Go 应用代码:

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)([]byte, 错误 )

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

在这条线上

响应,错误 := getJsonRes()

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

响应,错误 := getJsonRes(r)

关于go - 调用中没有足够的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30268749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com