mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 04:36:02 +03:00
added letter example with channel
This commit is contained in:
parent
edcdcb9456
commit
f887bcedec
35
concurrency/mastering_concurrency_in_go/ch1/iorem_ipsum.go
Normal file
35
concurrency/mastering_concurrency_in_go/ch1/iorem_ipsum.go
Normal file
@ -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)
|
||||
// }
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -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()
|
||||
}
|
||||
|
23
concurrency/select_block/main.go
Normal file
23
concurrency/select_block/main.go
Normal file
@ -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)
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user