1
0
mirror of https://github.com/tmrts/go-patterns.git synced 2024-11-25 06:26:06 +03:00

idiom/functional-options: Unexport options

Options is an implementation detail and adds no value by being exported.
By unexporting Options we decrease the API surface and maintain the
ability to make changes without breaking user expectations.
This commit is contained in:
Mathias Fredriksson 2018-01-06 02:35:57 +02:00
parent f978e42036
commit 4cdebe4220

View File

@ -10,7 +10,7 @@ Options implemented as a function set the state of that option.
```go ```go
package file package file
type Options struct { type options struct {
UID int UID int
GID int GID int
Flags int Flags int
@ -18,28 +18,28 @@ type Options struct {
Permissions os.FileMode Permissions os.FileMode
} }
type Option func(*Options) type Option func(*options)
func UID(userID int) Option { func UID(userID int) Option {
return func(args *Options) { return func(args *options) {
args.UID = userID args.UID = userID
} }
} }
func GID(groupID int) Option { func GID(groupID int) Option {
return func(args *Options) { return func(args *options) {
args.GID = groupID args.GID = groupID
} }
} }
func Contents(c string) Option { func Contents(c string) Option {
return func(args *Options) { return func(args *options) {
args.Contents = c args.Contents = c
} }
} }
func Permissions(perms os.FileMode) Option { func Permissions(perms os.FileMode) Option {
return func(args *Options) { return func(args *options) {
args.Permissions = perms args.Permissions = perms
} }
} }
@ -51,8 +51,8 @@ func Permissions(perms os.FileMode) Option {
package file package file
func New(filepath string, setters ...Option) error { func New(filepath string, setters ...Option) error {
// Default Options // Default options
args := &Options{ args := &options{
UID: os.Getuid(), UID: os.Getuid(),
GID: os.Getgid(), GID: os.Getgid(),
Contents: "", Contents: "",