diff --git a/functional_options.go b/functional_options.go new file mode 100644 index 0000000..198717d --- /dev/null +++ b/functional_options.go @@ -0,0 +1,65 @@ +package main + +import "os" + +type arguments struct { + UID int + GID int + Flags int + Contents string + Permissions os.FileMode +} + +type argument func(*arguments) + +func UID(userID int) argument { + return func(args *arguments) { + args.UID = userID + } +} + +func GID(groupID int) argument { + return func(args *arguments) { + args.GID = groupID + } +} + +func Contents(c string) argument { + return func(args *arguments) { + args.Contents = c + } +} + +func Permissions(perms os.FileMode) argument { + return func(args *arguments) { + args.Permissions = perms + } +} + +func New(filepath string, setters ...argument) error { + // Default arguments + args := &arguments{ + UID: os.Getuid(), + GID: os.Getgid(), + Contents: "", + Permissions: 0666, + Flags: os.O_CREATE | os.O_EXCL | os.O_WRONLY, + } + + for _, setter := range setters { + setter(args) + } + + f, err := os.OpenFile(filepath, args.Flags, args.Permissions) + if err != nil { + return err + } else { + defer f.Close() + } + + if _, err := f.WriteString(args.Contents); err != nil { + return err + } + + return f.Chown(args.UID, args.GID) +} diff --git a/strategy.go b/strategy.go new file mode 100644 index 0000000..9ef0c6e --- /dev/null +++ b/strategy.go @@ -0,0 +1,39 @@ +package main + +import "fmt" + +type Operator interface { + Apply(int, int) int +} + +type Operation struct { + Operator Operator +} + +func (o *Operation) Operate(leftValue, rightValue int) int { + return o.Operator.Apply(leftValue, rightValue) +} + +type Multiplication struct{} + +func (Multiplication) Apply(lval, rval int) int { + return lval * rval +} + +type Addition struct{} + +func (Addition) Apply(lval, rval int) int { + return lval + rval +} + +func main() { + mult := Operation{Multiplication{}} + + // Outputs 15 + fmt.Println(mult.Operate(3, 5)) + + pow := Operation{Addition{}} + + // Outputs 8 + fmt.Println(pow.Operate(3, 5)) +} diff --git a/template.go b/template.go deleted file mode 100644 index d9563ea..0000000 --- a/template.go +++ /dev/null @@ -1,48 +0,0 @@ -// In Template pattern, an abstract struct exposes defined way(s)/template(s) -// to execute its methods. This pattern comes under behavior pattern category. -package main - -import ( - "fmt" - "math" -) - -type Operator interface { - Apply(int, int) int -} - -type Operation struct { - Operator Operator -} - -func (o *Operation) Operate(leftValue, rightValue int) int { - return o.Operator.Apply(leftValue, rightValue) -} - -type Multiplication struct{} - -func (_ Multiplication) Apply(lval, rval int) int { - return lval * rval -} - -type Pow struct{} - -func (_ Pow) Apply(lval, rval int) int { - return int(math.Pow(float64(lval), float64(rval))) -} - -func main() { - mult := Operation{ - Operator: Multiplication{}, - } - - // Outputs 15 - fmt.Println(mult.Operate(3, 5)) - - pow := Operation{ - Operator: Pow{}, - } - - // Outputs 243 - fmt.Println(pow.Operate(3, 5)) -}