2017-07-19 46 views

回答

6

它在Go os軟件包源代碼註釋中進行了說明。

例如,這是安全的:

package main 

import "os" 

func main() { 
    f, err := os.Create("/tmp/atestfile") 
    if err != nil { 
     *f = os.File{} 
    } 
    // finalizer runs 
} 

軟件包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 
}