为什么Golang的文件结构设计成这样?

huangapple go评论62阅读模式
英文:

Why golang File struct design like this

问题

Golang的File结构如下所示:

type File struct {
    *file
}

而File结构的函数也被设计为接收一个指针,为什么要这样设计呢?

英文:

golang File struct is like this:

type File struct{
    *file
}

and File struct functiona is also design to recive a pointer,why it design like this?

答案1

得分: 8

这是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 表示一个打开的文件描述符。
type File struct {
*file // 特定于操作系统
}

go/src/os/file_plan9.go:

// file 是 *File 的真实表示。
// 额外的间接级别确保 os 的任何客户端都不能覆盖这些数据,否则可能导致 finalizer 关闭错误的文件描述符。
type file struct {
fd int
name string
dirinfo *dirInfo // 除非正在读取目录,否则为 nil
}

go/src/os/file_unix.go:

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

// file 是 *File 的真实表示。
// 额外的间接级别确保 os 的任何客户端都不能覆盖这些数据,否则可能导致 finalizer 关闭错误的文件描述符。
type file struct {
pfd poll.FD
name string
dirinfo *dirInfo // 除非正在读取目录,否则为 nil
nonblock bool // 是否设置非阻塞模式
}

go/src/os/file_windows.go:

// file 是 *File 的真实表示。
// 额外的间接级别确保 os 的任何客户端都不能覆盖这些数据,否则可能导致 finalizer 关闭错误的文件描述符。
type file struct {
pfd poll.FD
name string
dirinfo *dirInfo // 除非正在读取目录,否则为 nil
}

英文:

It is explained in the Go os package source code comments.

For example, this is safe:

package main

import "os"

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

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

huangapple
  • 本文由 发表于 2017年7月19日 10:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/45180082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定