concurrent goroutines
This commit is contained in:
		
							
								
								
									
										12
									
								
								gobyexample/channel-buffering.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								gobyexample/channel-buffering.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	messages := make(chan string, 2)
 | 
			
		||||
	messages <- "buffered"
 | 
			
		||||
	messages <- "channel"
 | 
			
		||||
 | 
			
		||||
	fmt.Println(<-messages)
 | 
			
		||||
	fmt.Println(<-messages)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										20
									
								
								gobyexample/channel-directions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								gobyexample/channel-directions.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func ping(pings chan<- string, msg string) {
 | 
			
		||||
	pings <- msg
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func pong(pings <-chan string, pongs chan<- string) {
 | 
			
		||||
	msg := <-pings
 | 
			
		||||
	pongs <- msg
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	pings := make(chan string, 1)
 | 
			
		||||
	pongs := make(chan string, 1)
 | 
			
		||||
	ping(pings, "passed message")
 | 
			
		||||
	pong(pings, pongs)
 | 
			
		||||
	fmt.Println(<-pongs)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								gobyexample/channel-synchronization.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								gobyexample/channel-synchronization.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func worker(done chan bool) {
 | 
			
		||||
	fmt.Print("working...")
 | 
			
		||||
	time.Sleep(time.Second)
 | 
			
		||||
	fmt.Println("done")
 | 
			
		||||
 | 
			
		||||
	done <- true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	done := make(chan bool, 1)
 | 
			
		||||
	go worker(done)
 | 
			
		||||
 | 
			
		||||
	<-done
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								gobyexample/channels.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								gobyexample/channels.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	messages := make(chan string)
 | 
			
		||||
 | 
			
		||||
	go func() { messages <- "ping" }()
 | 
			
		||||
 | 
			
		||||
	msg := <-messages
 | 
			
		||||
	fmt.Println(msg)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								gobyexample/goroutines.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								gobyexample/goroutines.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func f(from string) {
 | 
			
		||||
	for i := 0; i < 3; i++ {
 | 
			
		||||
		fmt.Println(from, ":", i)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	f("direct")
 | 
			
		||||
 | 
			
		||||
	go f("goroutine")
 | 
			
		||||
 | 
			
		||||
	go func(msg string) {
 | 
			
		||||
		fmt.Println(msg)
 | 
			
		||||
	}("going")
 | 
			
		||||
 | 
			
		||||
	time.Sleep(time.Second)
 | 
			
		||||
	fmt.Println("done")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								gobyexample/select.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								gobyexample/select.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,30 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	c1 := make(chan string)
 | 
			
		||||
	c2 := make(chan string)
 | 
			
		||||
 | 
			
		||||
	go func() {
 | 
			
		||||
		time.Sleep(1 * time.Second)
 | 
			
		||||
		c1 <- "one"
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	go func() {
 | 
			
		||||
		time.Sleep(2 * time.Second)
 | 
			
		||||
		c1 <- "two"
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	for i := 0; i < 2; i++ {
 | 
			
		||||
		select {
 | 
			
		||||
			case msg1 := <-c1:
 | 
			
		||||
				fmt.Println("received", msg1)
 | 
			
		||||
			case msg2 := <-c2:
 | 
			
		||||
				fmt.Println("received", msg2)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								gobyexample/waitgroups.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								gobyexample/waitgroups.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,29 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func worker(id int) {
 | 
			
		||||
	fmt.Printf("Worker %d starting\n", id)
 | 
			
		||||
	time.Sleep(time.Second)
 | 
			
		||||
	fmt.Printf("Worker %d done\n", id)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	var wg sync.WaitGroup
 | 
			
		||||
 | 
			
		||||
	for i:= 1; i<=5; i++ {
 | 
			
		||||
		wg.Add(1)
 | 
			
		||||
 | 
			
		||||
		i := i
 | 
			
		||||
 | 
			
		||||
		go func() {
 | 
			
		||||
			defer wg.Done()
 | 
			
		||||
			worker(i)
 | 
			
		||||
		}()
 | 
			
		||||
	}
 | 
			
		||||
	wg.Wait()
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user