Created
February 10, 2018 03:58
-
-
Save tomtsang/4ddcc171bd52b3f0d62fee24219bbcf9 to your computer and use it in GitHub Desktop.
struct-factory-method.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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