From edcdcb945646845e67c09a32b71f7c9515fbb099 Mon Sep 17 00:00:00 2001 From: Jian Han Date: Sat, 6 Jan 2018 23:30:19 +1000 Subject: [PATCH] added shape factory method --- .../mastering_concurrency_in_go/ch1/ch1.go | 38 ++++++++++++++++ .../mastering_concurrency_in_go/main.go | 7 +++ creational/factorymethod/shape/shape.go | 43 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 concurrency/mastering_concurrency_in_go/ch1/ch1.go create mode 100644 concurrency/mastering_concurrency_in_go/main.go create mode 100644 creational/factorymethod/shape/shape.go diff --git a/concurrency/mastering_concurrency_in_go/ch1/ch1.go b/concurrency/mastering_concurrency_in_go/ch1/ch1.go new file mode 100644 index 0000000..8175ee8 --- /dev/null +++ b/concurrency/mastering_concurrency_in_go/ch1/ch1.go @@ -0,0 +1,38 @@ +package ch1 + +import ( + "fmt" + "sync" + "time" +) + +type Job struct { + i int + max int + text string +} + +func outputText(j *Job, goGroup *sync.WaitGroup) { + for j.i < j.max { + time.Sleep(1 * time.Millisecond) + fmt.Println(j.text) + j.i++ + } + goGroup.Done() +} +func Run() { + goGroup := new(sync.WaitGroup) + fmt.Println("Starting") + hello := new(Job) + hello.text = "hello" + hello.i = 0 + hello.max = 2 + world := new(Job) + world.text = "world" + world.i = 0 + world.max = 2 + go outputText(hello, goGroup) + go outputText(world, goGroup) + goGroup.Add(2) + goGroup.Wait() +} diff --git a/concurrency/mastering_concurrency_in_go/main.go b/concurrency/mastering_concurrency_in_go/main.go new file mode 100644 index 0000000..4653db0 --- /dev/null +++ b/concurrency/mastering_concurrency_in_go/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/jianhan/go-patterns/concurrency/mastering_concurrency_in_go/ch1" + +func main() { + ch1.Run() +} diff --git a/creational/factorymethod/shape/shape.go b/creational/factorymethod/shape/shape.go new file mode 100644 index 0000000..1a2584c --- /dev/null +++ b/creational/factorymethod/shape/shape.go @@ -0,0 +1,43 @@ +package shape + +import ( + "errors" + + "github.com/davecgh/go-spew/spew" +) + +type Shape interface { + Draw() +} + +type Circle struct { +} + +func (c *Circle) Draw() { + spew.Dump("Draw Circle") +} + +type Square struct { +} + +func (s *Square) Draw() { + spew.Dump("Draw Square") +} + +type Rec struct { +} + +func (r *Rec) Draw() { + spew.Dump("Draw Rec") +} + +func GetShape(shape string) (Shape, error) { + if shape == "Circle" { + return &Circle{}, nil + } else if shape == "Square" { + return &Square{}, nil + } else if shape == "Rec" { + return &Rec{}, nil + } + return nil, errors.New("Shape not found") +}