Golang将变量按引用传递给结构体成员值

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

Golang passing a variable by reference to a structure member value

问题

我遇到了一个问题,无法通过引用或传递指针将值传递给结构体。我将概述我想要实现的内容:

type FooStruct struct {
    foo1, foo2, foo3 int //等等
    connection *net.Conn
}

func (session FooStruct) Run(conn *net.Conn) {
    session.connection = conn
    session.connection.RemoteAddr()
    ......
}

func main() {
    server, err := net.Listen("tcp", ":8484")
    connection, err := server.Accept()
    foo := FooStruct{}
    foo.Run(&connection)
}

上面是我尝试实现的示例,我只想将一个引用指针传递给结构体中的connection变量。我尝试阅读文档和教程,但我感到困惑。

在编译时,我收到错误消息- session.connection.RemoteAddr未定义(*net.Conn类型没有RemoteAddr字段或方法)。当复制变量时,它确实有效。但这不是我想要做的。

英文:

I am having difficulties passing a value to a struct by reference or by passing a pointer. I will outline what I am trying to achieve:

type FooStruct struct {
    foo1, foo2, foo3 int //etc
    connection *net.Conn
}

func(session FooStruct) Run(conn *net.Conn) {
    session.connection = conn
    session.connection.RemoteAddr()
    ......
}

func main() {
    server, err := net.Listen("tcp", ":8484")
    connection, err := server.Accept()
    foo := FooStruct{}
    foo.Run(&connection)
}

The above is an example of what I am trying to achive I only want to pass a reference pointer to the connection variable in the struct. I have tried reading the documentation and going through the tutorial but I have become confused.

When compiling I get the error - session.connection.RemoteAddr undefined (type * net.Conn has no field or method RemoteAddr). It does have that method as when copying the variable it works fine. However that is not what I want to do.

答案1

得分: 11

由于net.Conn是一个接口而不是一个结构体,你应该直接传递和存储它。像这样:

type FooStruct struct {
    foo1, foo2, foo3 int
    connection       net.Conn
}

func (session *FooStruct) Run(conn net.Conn) {
    session.connection = conn
    session.connection.RemoteAddr()
}

func main() {
    server, err := net.Listen("tcp", ":8484")
    connection, err := server.Accept()
    foo := FooStruct{}
    foo.Run(connection)
}

另请注意,我将Run方法的接收者更改为指针,这通常是你想要的

另外,参考Go FAQ中关于将接口传递给指针的问题

英文:

Since net.Conn is an interface, not a struct, you should pass and store it directly. Like this:

type FooStruct struct {
    foo1, foo2, foo3 int
    connection       net.Conn
}

func(session *FooStruct) Run(conn net.Conn) {
    session.connection = conn
    session.connection.RemoteAddr()
}

func main() {
    server, err := net.Listen("tcp", ":8484")
    connection, err := server.Accept()
    foo := FooStruct{}
    foo.Run(connection)
}

See also the Go FAQ entry on passing interfaces to pointers.

Also note that I changed the receiver of the Run method to a pointer, which is generally what you want.

huangapple
  • 本文由 发表于 2014年5月31日 06:12:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/23964100.html
匿名

发表评论

匿名网友

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

确定