英文:
Not enough arguments in call
问题
我正在尝试创建一个Go应用程序,该应用程序将显示用户的IP地址。
我无法解决我的日志控制台错误:
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, err := getJsonRes(r)
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, "", " ")
}
以上是您要翻译的内容。
英文:
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, "", " ")
}
答案1
得分: 5
你的函数
func getJsonRes(r *http.Request)([]byte, error )
接受一个请求指针,并返回一个字节数组和一个错误。
在这一行
response, err := getJsonRes()
你没有传入任何参数调用它。你可能想要做以下更改
response, err := getJsonRes(r)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论