1. 程式人生 > >go語言之初始化的建構函式

go語言之初始化的建構函式

// 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
}

初始化相當方便。