相关疑难解决方法(0)

为什么Go的构造函数会返回地址?

我知道Go没有任何构造函数,并且New func在它的位置使用了a ,但是根据这个例子.

func NewFile(fd int, name string) *File {
  if fd < 0 {
    return nil
  }
  f := File{fd, name, nil, 0}
  return &f
}
Run Code Online (Sandbox Code Playgroud)

他们总是回来&f.为什么只是回归File是不够的?

更新

我已经尝试将创建的对象返回到一个简单的结构,它很好.所以,我想知道返回地址是否是构造函数的标准方法.

谢谢.

constructor pointers go

6
推荐指数
1
解决办法
1477
查看次数

标签 统计

constructor ×1

go ×1

pointers ×1