package mainfunc main() {// create new channel of type intch := make(chan int)// start new anonymous goroutinego func() {// send 42 to channelch <- 42}()// read from channel<-ch}

package mainimport "time"func timer(d time.Duration) <-chan int {c := make(chan int)go func() {time.Sleep(d)c <- 1}()return c}func main() {for i := 0; i < 24; i++ {c := timer(1 * time.Second)<-c}}

package mainimport "time"func main() {var Ball inttable := make(chan int)go player(table)go player(table)table <- Balltime.Sleep(1 * time.Second)<-table}func player(table chan int) {for {ball := <-tableball++time.Sleep(100 * time.Millisecond)table <- ball}}

go player(table)go player(table)go player(table)

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

package mainimport ("fmt""time")func producer(ch chan int, d time.Duration) {var i intfor {ch <- ii++time.Sleep(d)}}func reader(out chan int) {for x := range out {fmt.Println(x)}}func main() {ch := make(chan int)out := make(chan int)go producer(ch, 100*time.Millisecond)go producer(ch, 250*time.Millisecond)go reader(out)for i := range ch {out <- i}}

package mainimport ("fmt""sync""time")func worker(tasksCh <-chan int, wg *sync.WaitGroup) {defer wg.Done()for {task, ok := <-tasksChif !ok {return}d := time.Duration(task) * time.Millisecondtime.Sleep(d)fmt.Println("processing task", task)}}func pool(wg *sync.WaitGroup, workers, tasks int) {tasksCh := make(chan int)for i := 0; i < workers; i++ {go worker(tasksCh, wg)}for i := 0; i < tasks; i++ {tasksCh <- i}close(tasksCh)}func main() {var wg sync.WaitGroupwg.Add(36)go pool(&wg, 36, 50)wg.Wait()}

package mainimport ("fmt""sync""time")const (WORKERS = 5SUBWORKERS = 3TASKS = 20SUBTASKS = 10)func subworker(subtasks chan int) {for {task, ok := <-subtasksif !ok {return}time.Sleep(time.Duration(task) * time.Millisecond)fmt.Println(task)}}func worker(tasks <-chan int, wg *sync.WaitGroup) {defer wg.Done()for {task, ok := <-tasksif !ok {return}subtasks := make(chan int)for i := 0; i < SUBWORKERS; i++ {go subworker(subtasks)}for i := 0; i < SUBTASKS; i++ {task1 := task * isubtasks <- task1}close(subtasks)}}func main() {var wg sync.WaitGroupwg.Add(WORKERS)tasks := make(chan int)for i := 0; i < WORKERS; i++ {go worker(tasks, &wg)}for i := 0; i < TASKS; i++ {tasks <- i}close(tasks)wg.Wait()}

package mainimport "net"func handler(c net.Conn) {c.Write([]byte("ok"))c.Close()}func main() {l, err := net.Listen("tcp", ":5000")if err != nil {panic(err)}for {c, err := l.Accept()if err != nil {continue}go handler(c)}}

package mainimport ("fmt""net""time")func handler(c net.Conn, ch chan string) {ch <- c.RemoteAddr().String()c.Write([]byte("ok"))c.Close()}func logger(ch chan string) {for {fmt.Println(<-ch)}}func server(l net.Listener, ch chan string) {for {c, err := l.Accept()if err != nil {continue}go handler(c, ch)}}func main() {l, err := net.Listen("tcp", ":5000")if err != nil {panic(err)}ch := make(chan string)go logger(ch)go server(l, ch)time.Sleep(10 * time.Second)}
package mainimport ("net""time")func handler(c net.Conn, ch chan string) {addr := c.RemoteAddr().String()ch <- addrtime.Sleep(100 * time.Millisecond)c.Write([]byte("ok"))c.Close()}func logger(wch chan int, results chan int) {for {data := <-wchdata++results <- data}}func parse(results chan int) {for {<-results}}func pool(ch chan string, n int) {wch := make(chan int)results := make(chan int)for i := 0; i < n; i++ {go logger(wch, results)}go parse(results)for {addr := <-chl := len(addr)wch <- l}}func server(l net.Listener, ch chan string) {for {c, err := l.Accept()if err != nil {continue}go handler(c, ch)}}func main() {l, err := net.Listen("tcp", ":5000")if err != nil {panic(err)}ch := make(chan string)go pool(ch, 4)go server(l, ch)time.Sleep(10 * time.Second)}
// A concurrent prime sievepackage mainimport "fmt"// Send the sequence 2, 3, 4, ... to channel 'ch'.func Generate(ch chan<- int) {for i := 2; ; i++ {ch <- i // Send 'i' to channel 'ch'.}}// Copy the values from channel 'in' to channel 'out',// removing those divisible by 'prime'.func Filter(in <-chan int, out chan<- int, prime int) {for {i := <-in // Receive value from 'in'.if i%prime != 0 {out <- i // Send 'i' to 'out'.}}}// The prime sieve: Daisy-chain Filter processes.func main() {ch := make(chan int) // Create a new channel.go Generate(ch) // Launch Generate goroutine.for i := 0; i < 10; i++ {prime := <-chfmt.Println(prime)ch1 := make(chan int)go Filter(ch, ch1, prime)ch = ch1}}
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!










评论