From c7083f902853e64ddf6e326be2c41fe7ff5ec769 Mon Sep 17 00:00:00 2001 From: oscarzhou Date: Mon, 24 Jan 2022 19:41:33 +1300 Subject: [PATCH] add more concurrent programming practices --- 2-compress-file/main.go | 1 + 3-word-counter/main.go | 79 +++++++++++++++++++++++++++++++++++++ 4-multi-channels/main.go | 40 +++++++++++++++++++ 5-close-channels/main.go | 70 ++++++++++++++++++++++++++++++++ 6-lock-with-channel/main.go | 28 +++++++++++++ interview-questions.md | 19 +++++++++ 6 files changed, 237 insertions(+) create mode 100644 3-word-counter/main.go create mode 100644 4-multi-channels/main.go create mode 100644 5-close-channels/main.go create mode 100644 6-lock-with-channel/main.go create mode 100644 interview-questions.md diff --git a/2-compress-file/main.go b/2-compress-file/main.go index d8704ae..3798d81 100644 --- a/2-compress-file/main.go +++ b/2-compress-file/main.go @@ -13,6 +13,7 @@ import ( 1. Practice gzip compress 2. How to use wait group 3. os.Args[1:] can stream in multiple files to the program without writing a walk function. Command: go run main.go ./testdata/* + 4. Passing the filename to each goroutine can make sure the correct file is executed as it's scheduled */ func main() { diff --git a/3-word-counter/main.go b/3-word-counter/main.go new file mode 100644 index 0000000..c2beece --- /dev/null +++ b/3-word-counter/main.go @@ -0,0 +1,79 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strings" + "sync" +) + +/* + + @Ref: https://livebook.manning.com/book/go-in-practice/chapter-3/65 + 1. --reac flag is able to do the race detection + 2. How to use scanner to split the word in a file + 3. sync.Mutex usage +*/ + +func main() { + + wg := sync.WaitGroup{} + + w := newWords() + for _, file := range os.Args[1:] { + wg.Add(1) + go func(filename string) { + tallyWords(filename, w) + wg.Done() + }(file) + + } + + wg.Wait() + + fmt.Println("Words that appear more than once:") + for word, count := range w.found { + if count > 1 { + fmt.Printf("%s: %d\n", word, count) + } + } +} + +type words struct { + sync.Mutex + found map[string]int +} + +func newWords() *words { + return &words{found: make(map[string]int)} +} + +func (w *words) add(word string, n int) { + w.Lock() + defer w.Unlock() + + count, ok := w.found[word] + if !ok { + w.found[word] = n + return + } + w.found[word] = count + n +} + +func tallyWords(filename string, dist *words) error { + file, err := os.Open(filename) + if err != nil { + return err + } + + defer file.Close() + + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanWords) + for scanner.Scan() { + word := strings.ToLower(scanner.Text()) + dist.add(word, 1) + } + return scanner.Err() +} diff --git a/4-multi-channels/main.go b/4-multi-channels/main.go new file mode 100644 index 0000000..4b43f8d --- /dev/null +++ b/4-multi-channels/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "os" + "time" +) + +/* + @Ref: https://livebook.manning.com/book/go-in-practice/chapter-3/65 + 1. time.After returns a channel + 2. channel usage + 3. select usage + 4. channel declaration with specifying a direction +*/ + +func main() { + done := time.After(30 * time.Second) + echo := make(chan []byte) + go readStdin(echo) + for { + select { + case buf := <-echo: + os.Stdout.Write(buf) + case <-done: + fmt.Println("Time out") + os.Exit(0) + } + } +} + +func readStdin(out chan<- []byte) { + for { + data := make([]byte, 1024) + l, _ := os.Stdin.Read(data) + if l > 0 { + out <- data + } + } +} diff --git a/5-close-channels/main.go b/5-close-channels/main.go new file mode 100644 index 0000000..78bf0a6 --- /dev/null +++ b/5-close-channels/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "time" +) + +/* + @Ref: https://livebook.manning.com/book/go-in-practice/chapter-3/65 +*/ + +func main() { + msg := make(chan string) + done := make(chan bool) + until := time.After(5 * time.Second) + + go send(msg, done) + + for { + select { + case m := <-msg: + fmt.Println(m) + case <-until: + done <- true + time.Sleep(500 * time.Millisecond) + return + } + } +} + +func send(ch chan<- string, done <-chan bool) { + for { + select { + case <-done: + fmt.Println("done") + close(ch) + return + default: + ch <- "Hello" + time.Sleep(500 * time.Millisecond) + + } + } +} + +// Improper channel close +// func main() { +// msg := make(chan string) +// until := time.After(5 * time.Second) + +// go send(msg) + +// for { +// select { +// case m := <-msg: +// fmt.Println(m) +// case <-until: +// close(msg) +// time.Sleep(500 * time.Millisecond) +// return +// } +// } +// } + +// func send(ch chan string) { +// for { +// ch <- "hello" +// time.Sleep(500 * time.Millisecond) +// } +// } diff --git a/6-lock-with-channel/main.go b/6-lock-with-channel/main.go new file mode 100644 index 0000000..523e150 --- /dev/null +++ b/6-lock-with-channel/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "time" +) + +/* + @Ref: https://livebook.manning.com/book/go-in-practice/chapter-3/65 +*/ + +func main() { + lock := make(chan bool, 1) + for i := 1; i < 7; i++ { + go worker(i, lock) + } + + time.Sleep(10 * time.Second) +} + +func worker(id int, lock chan bool) { + fmt.Printf("%d wants the lock\n", id) + lock <- true + fmt.Printf("%d has the lock\n", id) + time.Sleep(500 * time.Millisecond) + fmt.Printf("%d is releasing the lock\n", id) + <-lock +} diff --git a/interview-questions.md b/interview-questions.md new file mode 100644 index 0000000..fbe096d --- /dev/null +++ b/interview-questions.md @@ -0,0 +1,19 @@ +# Interview Questions + + +This doc is not a buntch of real questions from interview. It is more about the different concept in golang programming that could be asked by interviewers. + +## 1. What is goroutines? + +Goroutine, for me, I understand it as a detached process. It can run independently of the function that started it. + +## 2. What is channels? + +A channel is a pipeline for sending and receiving data. It is like a socket that runs inside your program. It provides a way for one goroutine to send structured data to another. + +## 3. What is wait group? + + + +## 4. Race condition +In a rece condition, two things are "racing" to use the same piece of data. Problems arise when both are working with the same data at around the same time. One goroutine may be only partway through modifying a value when another goroutine tries to use it. And that situation can have unintended consequences. \ No newline at end of file