mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 04:36:02 +03:00
added lexical demo
This commit is contained in:
parent
f90238c876
commit
d9d6a823f7
45
concurrency/confinement/lexical/main.go
Normal file
45
concurrency/confinement/lexical/main.go
Normal 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 we’re 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)
|
||||
}
|
36
interface-semantics/main.go
Normal file
36
interface-semantics/main.go
Normal 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 value’s 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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user