Go’s File
class if you look at the underlying implementation in go/src/os/types.go
is:
type File struct {
*file // os specific
}
I understand that this type is the point where the public facing user API and the internal implementation that differs depending on the OS meets. What remains unclear to me is how (or where) the runtime/compiler substitutes the *file
for particular OS implementation.
In the same os
package, file
is defined in file_<os>.go
.
For example, for UNIX systems, we have file_unix.go
, containing
// 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
stdoutOrErr bool // whether this is stdout or stderr
appendMode bool // whether file is opened for appending
}
See here the windows implementation in file_windows.go
.
The way the build tool chooses the correct file is configurable, and tracked in the build tool settings. This can be overwritten by GOOS
as an environment variable, or changing the config settings. The build uses GOOS
and GOARCH
and looks at filenames (among other things) to choose or ignore specific suffixes such as source_windows.go
.