为什么golang文件结构设计是这样的

时间:2017-07-19 02:45:31

标签: file go

golang文件结构是这样的:

type File struct{
    *file
}

和File struct functiona也设计为重现一个指针,为什么它这样设计?

1 个答案:

答案 0 :(得分:6)

在Go os包源代码注释中进行了解释。

例如,这是安全的:

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
}
  

包os

array.map