现在的位置: 首页 > 综合 > 正文

go语言之初始化的构造函数

2017年12月21日 ⁄ 综合 ⁄ 共 376字 ⁄ 字号 评论关闭

// File represents an open file descriptor.
type File struct {
    *file
}

// NewFile returns a new File with the given file descriptor and name.
func NewFile(fd uintptr, name string) *File {
    h := syscall.Handle(fd)
    if h == syscall.InvalidHandle {
        return nil
    }
    f := &File{&file{fd: h, name: name}} //初始化一个结构,返回地址 从复合声明中获取地址,意味着告诉编译器在堆中分配空间,而不是栈中。
    runtime.SetFinalizer(f.file, (*file).close)
    return f
}

初始化相当方便。

抱歉!评论已关闭.