File 为什么golang文件结构会这样设计

File 为什么golang文件结构会这样设计,file,go,File,Go,golang文件结构如下所示: type File struct{ *file } 文件结构函数也是为了接收指针而设计的,为什么它会这样设计?Go os包源代码注释中解释了这一点 例如,这是安全的: package main import "os" func main() { f, err := os.Create("/tmp/atestfile") if err != nil { *f = os.File{} } // finali

golang文件结构如下所示:

type File struct{
    *file
}

文件结构函数也是为了接收指针而设计的,为什么它会这样设计?

Go os包源代码注释中解释了这一点

例如,这是安全的:

package main

import "os"

func main() {
    f, err := os.Create("/tmp/atestfile")
    if err != nil {
        *f = os.File{}
    }
    // finalizer runs
}
软件包操作系统

go/src/os/types.go:

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

go/src/os/file_plan9.go:

// file is the real representation of *File.
// The extra level of indirection ensures that no clients of os
// can overwrite this data, which could cause the finalizer
// to close the wrong file descriptor.
type file struct {
  fd      int
  name    string
  dirinfo *dirInfo // nil unless directory being read
}

go/src/os/file_unix.go:

// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris

// file is the real representation of *File.
// The extra level of indirection ensures that no clients of os
// can overwrite this data, which could cause the finalizer
// to close the wrong file descriptor.
type file struct {
  pfd      poll.FD
  name     string
  dirinfo  *dirInfo // nil unless directory being read
  nonblock bool     // whether we set nonblocking mode
}

go/src/os/file_windows.go:

// file is the real representation of *File.
// The extra level of indirection ensures that no clients of os
// can overwrite this data, which could cause the finalizer
// to close the wrong file descriptor.
type file struct {
  pfd     poll.FD
  name    string
  dirinfo *dirInfo // nil unless directory being read
}

@YandryPozo:这并不能回答问题。