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)

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

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写