mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-21 20:36:01 +03:00
Merge branch 'master' of https://github.com/jianhan/go-patterns
This commit is contained in:
commit
04c011eb9d
BIN
concurrency/.DS_Store
vendored
Normal file
BIN
concurrency/.DS_Store
vendored
Normal file
Binary file not shown.
52
concurrency/basic_channel_error_handling/main.go
Normal file
52
concurrency/basic_channel_error_handling/main.go
Normal file
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func fetchAll() error {
|
||||
var N = 4
|
||||
quit := make(chan bool)
|
||||
errc := make(chan error)
|
||||
done := make(chan error)
|
||||
for i := 0; i < N; i++ {
|
||||
go func(i int) {
|
||||
// dummy fetch
|
||||
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
|
||||
err := error(nil)
|
||||
if rand.Intn(2) == 0 {
|
||||
err = fmt.Errorf("goroutine %d's error returned", i)
|
||||
}
|
||||
ch := done // we'll send to done if nil error and to errc otherwise
|
||||
if err != nil {
|
||||
ch = errc
|
||||
}
|
||||
select {
|
||||
case ch <- err:
|
||||
return
|
||||
case <-quit:
|
||||
return
|
||||
}
|
||||
}(i)
|
||||
}
|
||||
count := 0
|
||||
for {
|
||||
select {
|
||||
case err := <-errc:
|
||||
close(quit)
|
||||
return err
|
||||
case <-done:
|
||||
count++
|
||||
if count == N {
|
||||
return nil // got all N signals, so there was no error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
fmt.Println(fetchAll())
|
||||
}
|
@ -20,6 +20,7 @@ goroutines in some sort of organized fashion.
|
||||
**/
|
||||
|
||||
func main() {
|
||||
resourceLeak()
|
||||
cancellationSignal()
|
||||
}
|
||||
|
||||
|
65
concurrency/select_error_handling/main.go
Normal file
65
concurrency/select_error_handling/main.go
Normal file
@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := selectWithErrorDemo(); err != nil {
|
||||
spew.Dump(err)
|
||||
}
|
||||
time.Sleep(time.Second * 5)
|
||||
}
|
||||
|
||||
func selectWithErrorDemo() error {
|
||||
// start update race and market
|
||||
count, errChan, doneChan := 2, make(chan error), make(chan bool)
|
||||
|
||||
go func() {
|
||||
// update race
|
||||
if err := doFirstThing(); err != nil {
|
||||
errChan <- err
|
||||
} else {
|
||||
doneChan <- true
|
||||
fmt.Println("Finish First Go Routine")
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
go func() {
|
||||
// update market
|
||||
if err := doSecondThing(); err != nil {
|
||||
errChan <- err
|
||||
} else {
|
||||
doneChan <- true
|
||||
fmt.Println("Finish Second Go Routine")
|
||||
}
|
||||
}()
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
select {
|
||||
case err := <-errChan:
|
||||
return err
|
||||
case d := <-doneChan:
|
||||
spew.Dump(i, d)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func doFirstThing() error {
|
||||
time.Sleep(time.Second * 2)
|
||||
fmt.Println("Exectue Do First")
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func doSecondThing() error {
|
||||
time.Sleep(time.Second * 1)
|
||||
fmt.Println("Exectue Do Second")
|
||||
// return errors.New("Error In Second")
|
||||
return nil
|
||||
}
|
@ -1,8 +1,55 @@
|
||||
package main
|
||||
|
||||
import "github.com/jianhan/go-patterns/concurrency/subtasks/fetchers"
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type Dog struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
type House struct {
|
||||
person *Person
|
||||
dog *Dog
|
||||
}
|
||||
|
||||
func main() {
|
||||
// divide_and_conquer.RunDivideAndConquer()
|
||||
fetchers.RunFetchers()
|
||||
// fetchers.RunFetchers()
|
||||
// h := &House{}
|
||||
// spew.Dump(h)
|
||||
|
||||
// go func() {
|
||||
// defer fmt.Println("Returned")
|
||||
// for {
|
||||
// select {
|
||||
// default:
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
// fmt.Println("TEST")
|
||||
// }()
|
||||
// time.Sleep(time.Second)
|
||||
// spew.Dump(caller())
|
||||
// spew.Dump(nakedReturn())
|
||||
}
|
||||
|
||||
func nakedReturn() (i uint32, err error) {
|
||||
return 12, errors.New("Test")
|
||||
}
|
||||
|
||||
func caller() (d *Dog, err error) {
|
||||
p, err := produceErr()
|
||||
spew.Dump(p)
|
||||
return
|
||||
}
|
||||
|
||||
func produceErr() (*Person, error) {
|
||||
return &Person{Name: "James"}, errors.New("New Error")
|
||||
}
|
||||
|
22
idiom/iota_const/main.go
Normal file
22
idiom/iota_const/main.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "github.com/davecgh/go-spew/spew"
|
||||
|
||||
type FeedLockLinkType int
|
||||
|
||||
const (
|
||||
Racing FeedLockLinkType = iota
|
||||
Market
|
||||
)
|
||||
|
||||
var feedLockLinkTypes = [...]string{
|
||||
"racing.Races.raceID",
|
||||
"racing.Markets.marketID",
|
||||
}
|
||||
|
||||
func (f FeedLockLinkType) String() string { return feedLockLinkTypes[f] }
|
||||
|
||||
func main() {
|
||||
test := FeedLockLinkType(Racing).String()
|
||||
spew.Dump(test)
|
||||
}
|
Loading…
Reference in New Issue
Block a user