Skip to content

Instantly share code, notes, and snippets.

@tomtsang
Created February 10, 2018 03:58
Show Gist options
  • Select an option

  • Save tomtsang/4ddcc171bd52b3f0d62fee24219bbcf9 to your computer and use it in GitHub Desktop.

Select an option

Save tomtsang/4ddcc171bd52b3f0d62fee24219bbcf9 to your computer and use it in GitHub Desktop.
struct-factory-method.go
type File struct {
fd int // 文件描述符
name string // 文件名
}
// 下面是这个结构体类型对应的工厂方法,它返回一个指向结构体实例的指针:
func NewFile(fd int, name string) *File {
if fd < 0 {
return nil
}
return &File{fd, name}
}
// 然后这样调用它:
f := NewFile(10, "./test.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment