mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 04:36:02 +03:00
Added read struct property example
This commit is contained in:
parent
bbbcfc7977
commit
e8ffa49362
@ -1,46 +1,33 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// package shows if read/write concurrently via struct
|
// package shows if read/write concurrently via struct
|
||||||
|
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string
|
Name string
|
||||||
Age uint32
|
|
||||||
Hobbit string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Person) generateAge() {
|
// simulate write operation for struct property which is name is this case
|
||||||
// mock read
|
func (p *Person) updateName1() {
|
||||||
for {
|
for {
|
||||||
n := p.Name
|
p.Name = "Dummy Name 1"
|
||||||
spew.Dump(n)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Person) generateHobbit() {
|
|
||||||
// mock read
|
|
||||||
for {
|
|
||||||
n := p.Name
|
|
||||||
spew.Dump(n)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Person) mockWrite1() {
|
|
||||||
for {
|
|
||||||
p.Name = "Nmae1"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Person) mockWrite2() {
|
// simulate write operation for struct property which is name is this case
|
||||||
|
func (p *Person) updateName2() {
|
||||||
for {
|
for {
|
||||||
p.Name = "Nmae2"
|
p.Name = "Dummy Name 2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Person) printName() {
|
||||||
|
for {
|
||||||
|
fmt.Println("Current Name Is : ", p.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,10 +35,9 @@ var m = map[string]int{"a": 1}
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
p := &Person{Name: "James"}
|
p := &Person{Name: "James"}
|
||||||
go p.generateAge()
|
go p.updateName1()
|
||||||
go p.generateHobbit()
|
go p.updateName2()
|
||||||
go p.mockWrite1()
|
go p.printName()
|
||||||
go p.mockWrite2()
|
|
||||||
// SimulateConcurrentReadWriteMap()
|
// SimulateConcurrentReadWriteMap()
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user