added lexical demo

This commit is contained in:
jian.han 2018-01-16 11:32:21 +10:00
parent f90238c876
commit d9d6a823f7
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package main
import "fmt"
// Confinement is the simple yet powerful idea of ensuring information is only ever available from one concurrent process.
// There are two kinds of confinement possible: ad hoc and lexical.
// Lexical confinement involves using lexical scope to expose only the correct data and
// concurrency primitives for multiple concurrent processes to use. It makes it impossible to do the wrong thing.
func main() {
lexicalDemo()
}
func lexicalDemo() {
// Here we instantiate the channel within the lexical scope of the chanOwner function.
// This limits the scope of the write aspect of the results channel to the closure
// defined below it. In other words, it confines the write aspect of this channel to
// prevent other goroutines from writing to it.
chanOwner := func() <-chan int {
results := make(chan int, 5)
go func() {
defer close(results)
for i := 0; i <= 5; i++ {
results <- i
}
}()
return results
}
// Here we receive a read-only copy of an int channel. By declaring that the only
// usage we require is read access, we confine usage of the channel within the consume function to only reads
comsumer := func(results <-chan int) {
for result := range results {
fmt.Println("Received: %d\n", result)
}
fmt.Println("Done Receiving!")
}
// Here we receive the read aspect of the channel and were able to pass it into the
// consumer, which can do nothing but read from it. Once again this confines the
// main goroutine to a read-only view of the channel.
results := chanOwner()
comsumer(results)
}

View File

@ -0,0 +1,36 @@
package main
import "fmt"
// interface in Go provides both a value and pointer semantic form.
// An interface can store its own copy of a value (value semantics), or a value can be shared
// with the interface by storing a copy of the values address (pointer semantics).
// This is where the value/pointer semantics come in for interfaces
type printer interface {
print()
}
type user struct {
name string
}
func (u user) print() {
fmt.Println("User Name:", u.name)
}
func main() {
u := user{"Bill"}
entities := []printer{
u,
&u,
}
u.name = "Bill_CHG"
for _, e := range entities {
e.print()
}
}
func ptest(p printer) {
p.print()
}