dongnao9525 2017-01-09 20:07
浏览 95
已采纳

如何在GO中获取LocalAddress?

I'm making a webserver in go that act like a proxy. I need to get the infos about the client to give its response. Here is my code:

func main(){

    li, err := net.Listen("tcp", ":8000")
    if err != nil{
        log.Fatalln(err.Error())
    }
    defer li.Close()

    for{

        conn, err := li.Accept()
        if err != nil {
            log.Fatalln(err.Error())
        }
        local := conn.LocalAddr
        remote := conn.RemoteAddr
        fmt.Println(string(local.Network))
        fmt.Println(string(remote.String))

        go handleConn(conn)
    }
}

The problem is when i run i receive this message:

local.Network undefined (type func() net.Addr has no field or method Network)

but the documentation says the Addr type has this methods

https://golang.org/pkg/net/#Conn

https://golang.org/pkg/net/#Addr

  • 写回答

1条回答 默认 最新

  • dsyct08008 2017-01-09 20:11
    关注

    You are not calling the function, in your local variable you are storing the function itself.

    Try this:

    local := conn.LocalAddr()
    remote := conn.RemoteAddr()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器