doushansu9012 2015-05-15 21:09
浏览 16
已采纳

通话中参数不足

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条回答 默认 最新

  • duanjia9577 2015-05-15 21:12
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探