1. package main
    2. func main() {
    3. // create new channel of type int
    4. ch := make(chan int)
    5. // start new anonymous goroutine
    6. go func() {
    7. // send 42 to channel
    8. ch <- 42
    9. }()
    10. // read from channel
    11. <-ch
    12. }

    go并发案例 - 图1

    1. package main
    2. import "time"
    3. func timer(d time.Duration) <-chan int {
    4. c := make(chan int)
    5. go func() {
    6. time.Sleep(d)
    7. c <- 1
    8. }()
    9. return c
    10. }
    11. func main() {
    12. for i := 0; i < 24; i++ {
    13. c := timer(1 * time.Second)
    14. <-c
    15. }
    16. }

    go并发案例 - 图2

    1. package main
    2. import "time"
    3. func main() {
    4. var Ball int
    5. table := make(chan int)
    6. go player(table)
    7. go player(table)
    8. table <- Ball
    9. time.Sleep(1 * time.Second)
    10. <-table
    11. }
    12. func player(table chan int) {
    13. for {
    14. ball := <-table
    15. ball++
    16. time.Sleep(100 * time.Millisecond)
    17. table <- ball
    18. }
    19. }

    go并发案例 - 图3

    1. go player(table)
    2. go player(table)
    3. go player(table)

    go并发案例 - 图4

    1. for i := 0; i < 100; i++ {
    2. go player(table)
    3. }

    go并发案例 - 图5

    1. package main
    2. import (
    3. "fmt"
    4. "time"
    5. )
    6. func producer(ch chan int, d time.Duration) {
    7. var i int
    8. for {
    9. ch <- i
    10. i++
    11. time.Sleep(d)
    12. }
    13. }
    14. func reader(out chan int) {
    15. for x := range out {
    16. fmt.Println(x)
    17. }
    18. }
    19. func main() {
    20. ch := make(chan int)
    21. out := make(chan int)
    22. go producer(ch, 100*time.Millisecond)
    23. go producer(ch, 250*time.Millisecond)
    24. go reader(out)
    25. for i := range ch {
    26. out <- i
    27. }
    28. }

    go并发案例 - 图6

    1. package main
    2. import (
    3. "fmt"
    4. "sync"
    5. "time"
    6. )
    7. func worker(tasksCh <-chan int, wg *sync.WaitGroup) {
    8. defer wg.Done()
    9. for {
    10. task, ok := <-tasksCh
    11. if !ok {
    12. return
    13. }
    14. d := time.Duration(task) * time.Millisecond
    15. time.Sleep(d)
    16. fmt.Println("processing task", task)
    17. }
    18. }
    19. func pool(wg *sync.WaitGroup, workers, tasks int) {
    20. tasksCh := make(chan int)
    21. for i := 0; i < workers; i++ {
    22. go worker(tasksCh, wg)
    23. }
    24. for i := 0; i < tasks; i++ {
    25. tasksCh <- i
    26. }
    27. close(tasksCh)
    28. }
    29. func main() {
    30. var wg sync.WaitGroup
    31. wg.Add(36)
    32. go pool(&amp;wg, 36, 50)
    33. wg.Wait()
    34. }

    go并发案例 - 图7

    1. package main
    2. import (
    3. "fmt"
    4. "sync"
    5. "time"
    6. )
    7. const (
    8. WORKERS = 5
    9. SUBWORKERS = 3
    10. TASKS = 20
    11. SUBTASKS = 10
    12. )
    13. func subworker(subtasks chan int) {
    14. for {
    15. task, ok := <-subtasks
    16. if !ok {
    17. return
    18. }
    19. time.Sleep(time.Duration(task) * time.Millisecond)
    20. fmt.Println(task)
    21. }
    22. }
    23. func worker(tasks <-chan int, wg *sync.WaitGroup) {
    24. defer wg.Done()
    25. for {
    26. task, ok := <-tasks
    27. if !ok {
    28. return
    29. }
    30. subtasks := make(chan int)
    31. for i := 0; i < SUBWORKERS; i++ {
    32. go subworker(subtasks)
    33. }
    34. for i := 0; i < SUBTASKS; i++ {
    35. task1 := task * i
    36. subtasks <- task1
    37. }
    38. close(subtasks)
    39. }
    40. }
    41. func main() {
    42. var wg sync.WaitGroup
    43. wg.Add(WORKERS)
    44. tasks := make(chan int)
    45. for i := 0; i < WORKERS; i++ {
    46. go worker(tasks, &amp;wg)
    47. }
    48. for i := 0; i < TASKS; i++ {
    49. tasks <- i
    50. }
    51. close(tasks)
    52. wg.Wait()
    53. }

    go并发案例 - 图8

    1. package main
    2. import "net"
    3. func handler(c net.Conn) {
    4. c.Write([]byte("ok"))
    5. c.Close()
    6. }
    7. func main() {
    8. l, err := net.Listen("tcp", ":5000")
    9. if err != nil {
    10. panic(err)
    11. }
    12. for {
    13. c, err := l.Accept()
    14. if err != nil {
    15. continue
    16. }
    17. go handler(c)
    18. }
    19. }

    go并发案例 - 图9

    1. package main
    2. import (
    3. "fmt"
    4. "net"
    5. "time"
    6. )
    7. func handler(c net.Conn, ch chan string) {
    8. ch <- c.RemoteAddr().String()
    9. c.Write([]byte("ok"))
    10. c.Close()
    11. }
    12. func logger(ch chan string) {
    13. for {
    14. fmt.Println(<-ch)
    15. }
    16. }
    17. func server(l net.Listener, ch chan string) {
    18. for {
    19. c, err := l.Accept()
    20. if err != nil {
    21. continue
    22. }
    23. go handler(c, ch)
    24. }
    25. }
    26. func main() {
    27. l, err := net.Listen("tcp", ":5000")
    28. if err != nil {
    29. panic(err)
    30. }
    31. ch := make(chan string)
    32. go logger(ch)
    33. go server(l, ch)
    34. time.Sleep(10 * time.Second)
    35. }
    1. package main
    2. import (
    3. "net"
    4. "time"
    5. )
    6. func handler(c net.Conn, ch chan string) {
    7. addr := c.RemoteAddr().String()
    8. ch <- addr
    9. time.Sleep(100 * time.Millisecond)
    10. c.Write([]byte("ok"))
    11. c.Close()
    12. }
    13. func logger(wch chan int, results chan int) {
    14. for {
    15. data := <-wch
    16. data++
    17. results <- data
    18. }
    19. }
    20. func parse(results chan int) {
    21. for {
    22. <-results
    23. }
    24. }
    25. func pool(ch chan string, n int) {
    26. wch := make(chan int)
    27. results := make(chan int)
    28. for i := 0; i < n; i++ {
    29. go logger(wch, results)
    30. }
    31. go parse(results)
    32. for {
    33. addr := <-ch
    34. l := len(addr)
    35. wch <- l
    36. }
    37. }
    38. func server(l net.Listener, ch chan string) {
    39. for {
    40. c, err := l.Accept()
    41. if err != nil {
    42. continue
    43. }
    44. go handler(c, ch)
    45. }
    46. }
    47. func main() {
    48. l, err := net.Listen("tcp", ":5000")
    49. if err != nil {
    50. panic(err)
    51. }
    52. ch := make(chan string)
    53. go pool(ch, 4)
    54. go server(l, ch)
    55. time.Sleep(10 * time.Second)
    56. }
    1. // A concurrent prime sieve
    2. package main
    3. import "fmt"
    4. // Send the sequence 2, 3, 4, ... to channel 'ch'.
    5. func Generate(ch chan<- int) {
    6. for i := 2; ; i++ {
    7. ch <- i // Send 'i' to channel 'ch'.
    8. }
    9. }
    10. // Copy the values from channel 'in' to channel 'out',
    11. // removing those divisible by 'prime'.
    12. func Filter(in <-chan int, out chan<- int, prime int) {
    13. for {
    14. i := <-in // Receive value from 'in'.
    15. if i%prime != 0 {
    16. out <- i // Send 'i' to 'out'.
    17. }
    18. }
    19. }
    20. // The prime sieve: Daisy-chain Filter processes.
    21. func main() {
    22. ch := make(chan int) // Create a new channel.
    23. go Generate(ch) // Launch Generate goroutine.
    24. for i := 0; i < 10; i++ {
    25. prime := <-ch
    26. fmt.Println(prime)
    27. ch1 := make(chan int)
    28. go Filter(ch, ch1, prime)
    29. ch = ch1
    30. }
    31. }