diff --git a/concurrency/mastering_concurrency_in_go/ch1/iorem_ipsum.go b/concurrency/mastering_concurrency_in_go/ch1/iorem_ipsum.go new file mode 100644 index 0000000..7a42a53 --- /dev/null +++ b/concurrency/mastering_concurrency_in_go/ch1/iorem_ipsum.go @@ -0,0 +1,35 @@ +package ch1 + +// var initialString string +// var finalString string +// var stringLength int + +// func addToFinalStack(letterChannel chan string, wg *sync.WaitGroup) { +// letter := <-letterChannel +// finalString += letter +// wg.Done() +// } +// func capitalize(letterChannel chan string, currentLetter string, +// wg *sync.WaitGroup) { +// thisLetter := strings.ToUpper(currentLetter) +// wg.Done() +// letterChannel <- thisLetter +// } +// func RunLorem() { +// runtime.GOMAXPROCS(2) +// var wg sync.WaitGroup +// initialString = `Four score and seven years ago our fathers +// brought forth on this continent, a new nation, conceived in +// Liberty, and dedicated to the proposition that all men are +// created equal.` +// initialBytes := []byte(initialString) +// var letterChannel chan string = make(chan string) +// stringLength = len(initialBytes) +// for i := 0; i < stringLength; i++ { +// wg.Add(2) +// go capitalize(letterChannel, string(initialBytes[i]), &wg) +// go addToFinalStack(letterChannel, &wg) +// wg.Wait() +// } +// fmt.Println(finalString) +// } diff --git a/concurrency/mastering_concurrency_in_go/ch1/letter_channel_example.go b/concurrency/mastering_concurrency_in_go/ch1/letter_channel_example.go new file mode 100644 index 0000000..1437391 --- /dev/null +++ b/concurrency/mastering_concurrency_in_go/ch1/letter_channel_example.go @@ -0,0 +1,59 @@ +package ch1 + +import ( + "fmt" + "strings" + "sync" +) + +var initialString string +var initialBytes []byte +var stringLength int +var finalString string +var lettersProcessed int +var applicationStatus bool +var wg sync.WaitGroup + +func getLetters(gQ chan string) { + for i := range initialBytes { + gQ <- string(initialBytes[i]) + } +} +func capitalizeLetters(gQ chan string, sQ chan string) { + for { + if lettersProcessed >= stringLength { + applicationStatus = false + break + } + select { + case letter := <-gQ: + capitalLetter := strings.ToUpper(letter) + finalString += capitalLetter + lettersProcessed++ + } + } +} +func RunLetter() { + applicationStatus = true + getQueue := make(chan string) + stackQueue := make(chan string) + initialString = `Four score and seven years ago our fathers +brought forth on this continent, a new nation, conceived in +Liberty, and dedicated to the proposition that all men are +created equal.` + initialBytes = []byte(initialString) + stringLength = len(initialString) + lettersProcessed = 0 + fmt.Println("Let's start capitalizing") + go getLetters(getQueue) + capitalizeLetters(getQueue, stackQueue) + close(getQueue) + close(stackQueue) + for { + if applicationStatus == false { + fmt.Println("Done") + fmt.Println(finalString) + break + } + } +} diff --git a/concurrency/mastering_concurrency_in_go/main.go b/concurrency/mastering_concurrency_in_go/main.go index 4653db0..d468ac4 100644 --- a/concurrency/mastering_concurrency_in_go/main.go +++ b/concurrency/mastering_concurrency_in_go/main.go @@ -3,5 +3,6 @@ package main import "github.com/jianhan/go-patterns/concurrency/mastering_concurrency_in_go/ch1" func main() { - ch1.Run() + // ch1.Run() + ch1.RunLetter() } diff --git a/concurrency/select_block/main.go b/concurrency/select_block/main.go new file mode 100644 index 0000000..156c982 --- /dev/null +++ b/concurrency/select_block/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "time" + + "github.com/davecgh/go-spew/spew" +) + +func main() { + a := make(chan string) + go func() { + time.Sleep(time.Second * 2) + test := "string" + a <- test + }() + + select { + case t := <-a: + spew.Dump(t) + + } + +}