调用中参数不足

huangapple go评论68阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2015年5月16日 05:09:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/30268749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定