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