Go Web编程 Chapter9

Go web编程

Chapter_9 Go_Concurrency

channel_message
package main

import (
   "fmt"
   "time"
)

func thrower(c chan int) {
   for i := 0; i < 5; i++ {
      c <- i
      fmt.Println("Threw  >>", i)
   }
}

func catcher(c chan int) {
   for i := 0; i < 5; i++ {
      num := <-c
      fmt.Println("Caught <<", num)
   }
}

func main() {
   c := make(chan int, 3)
   go thrower(c)
   go catcher(c)
   time.Sleep(100 * time.Millisecond)
}
channel_select
package main

import (
   "fmt"
)

func callerA(c chan string) {
   c <- "Hello World!"
   close(c)
}

func callerB(c chan string) {
   c <- "Hola Mundo!"
   close(c)
}

func main() {
   a, b := make(chan string), make(chan string)
   go callerA(a)
   go callerB(b)
   var msg string
   openA, openB := true, true
   for openA || openB {
      select {
      case msg, openA = <-a:
         if openA {
            fmt.Printf("%s from A\n", msg)
         }
      case msg, openB = <-b:
         if openB {
            fmt.Printf("%s from B\n", msg)
         }
      }
   }
}

// func main() {
//     a, b := make(chan string), make(chan string)
//     go callerA(a)
//     go callerB(b)
//     msg1, msg2 := "A", "B"
//     for {
//        time.Sleep(1 * time.Microsecond)
//
//        select {
//        case msg1 = <-a:
//           fmt.Printf("%s from A\n", msg1)
//        case msg2 = <-b:
//           fmt.Printf("%s from B\n", msg2)
//        // default:
//        //     fmt.Println("Default")
//        }
//        if msg1 == "" && msg2 == "" {
//           break
//        }
//
//     }
// }
channel_shared
package main

import (
   "fmt"
   // "math/rand"
   "runtime"
   "time"
)

var DB Store

type Store struct {
   hash map[string]string
   in   chan [2]string
   out  chan [2]string
}

func StoreInit() {
   DB = Store{
      hash: make(map[string]string),
      in:   make(chan [2]string),
   }
   go func() {
      for {
         a := <-DB.in
         DB.hash[a[0]] = a[1]
      }
   }()
}

func (store *Store) Get(key string) (value string, err error) {
   value = store.hash[key]
   return
}

func (store *Store) Add(key string, value string) (err error) {
   a := [2]string{key, value}
   store.in <- a
   // store.hash[key] = value
   return
}

func (store *Store) Set(key string, value string) (err error) {

   return
}

func (store *Store) Del(key string) (err error) {

   return
}

func (store *Store) Pop(key string) (value string, err error) {

   return
}

func main() {
   runtime.GOMAXPROCS(4)
   StoreInit()
   for i := 0; i < 10; i++ {
      go DB.Add("a", "A")
      go DB.Add("a", "B")
      go DB.Add("a", "C")

      time.Sleep(1 * time.Microsecond)

      s, _ := DB.Get("a")
      fmt.Printf("%s ", s)

   }
}
channel_wait
package main

import "fmt"
import "time"

func printNumbers(w chan bool) {
	for i := 0; i < 10; i++ {
		time.Sleep(1 * time.Microsecond)
		fmt.Printf("%d ", i)
	}
	w <- true
}

func printLetters(w chan bool) {
	for i := 'A'; i < 'A'+10; i++ {
		time.Sleep(1 * time.Microsecond)
		fmt.Printf("%c ", i)
	}
	w <- true
}

func main() {
	w1, w2 := make(chan bool), make(chan bool)
	go printNumbers(w1)
	go printLetters(w2)
	<-w1
	<-w2
}
goroutine
package main

// import "fmt"
import "time"

func printNumbers1() {
	for i := 0; i < 10; i++ {
		// fmt.Printf("%d ", i)
	}
}

func printLetters1() {
	for i := 'A'; i < 'A'+10; i++ {
		// fmt.Printf("%c ", i)
	}
}

func printNumbers2() {
	for i := 0; i < 10; i++ {
		time.Sleep(1 * time.Microsecond)
		// fmt.Printf("%d ", i)
	}
}

func printLetters2() {
	for i := 'A'; i < 'A'+10; i++ {
		time.Sleep(1 * time.Microsecond)
		// fmt.Printf("%c ", i)
	}
}

func print1() {
	printNumbers1()
	printLetters1()
}

func goPrint1() {
	go printNumbers1()
	go printLetters1()
}

func print2() {
	printNumbers2()
	printLetters2()
}

func goPrint2() {
	go printNumbers2()
	go printLetters2()
}

func main() {
}
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy