updates
This commit is contained in:
parent
6750b72724
commit
618dec4fae
@ -1,3 +1,5 @@
|
|||||||
|
// ## New
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Structs
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Struct Fields
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Methods
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,7 +1,9 @@
|
|||||||
|
// ## Embedding
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
import "math"
|
import "math"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type Shape interface {
|
type Shape interface {
|
||||||
area() float64
|
area() float64
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Interfaces
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Goroutines
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,10 +1,10 @@
|
|||||||
|
// ## Concurrent Goroutines
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"fmt"
|
import "math/rand"
|
||||||
"time"
|
import "fmt"
|
||||||
"math/rand"
|
|
||||||
)
|
|
||||||
|
|
||||||
func f(n int) {
|
func f(n int) {
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Channels
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Channel Buffering
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## Channel Directions
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
@ -1,23 +1,32 @@
|
|||||||
package main // We can use channels to synchronize execution
|
// ## Synchronization
|
||||||
|
|
||||||
|
// We can use channels to synchronize execution
|
||||||
// accross goroutines. Here's an example of waiting
|
// accross goroutines. Here's an example of waiting
|
||||||
import "fmt" // for another goroutine to finish.
|
// for another goroutine to finish.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
func worker(done chan<- bool) { // The `done` channel will be used for
|
// The `done` channel will be used for
|
||||||
fmt.Print("Working...") // synchronization.
|
// synchronization.
|
||||||
|
func worker(done chan<- bool) {
|
||||||
|
fmt.Print("Working...")
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
fmt.Println(" done")
|
fmt.Println(" done")
|
||||||
done <- true // Send a value to notify that the work is done.
|
|
||||||
|
// Send a value to notify that the work is done.
|
||||||
|
done <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
done := make(chan bool, 1) // Start a worker goroutine, give it the channel to
|
// Start a worker goroutine, give it the channel to
|
||||||
go worker(done) // notify on.
|
// notify on.
|
||||||
|
done := make(chan bool, 1)
|
||||||
|
go worker(done)
|
||||||
|
|
||||||
<- done // Block until we can receive a value from the worker
|
// Block until we can receive a value from the worker
|
||||||
} // over the channel.
|
// over the channel.
|
||||||
|
<- done
|
||||||
/*
|
}
|
||||||
$ go run synchronization.go // If you commented out the `<- done` line, the
|
|
||||||
Working... done // program would exit before the `worker` even
|
|
||||||
*/ // started.
|
|
||||||
|
6
039-synchronization/synchronization.sh
Normal file
6
039-synchronization/synchronization.sh
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
$ go run synchronization.go
|
||||||
|
Working... done
|
||||||
|
|
||||||
|
# If you commented out the `<- done` line, the
|
||||||
|
# program would exit before the `worker` even
|
||||||
|
# started.
|
Loading…
x
Reference in New Issue
Block a user