added futrue

This commit is contained in:
Jian Han 2018-01-08 23:23:44 +10:00
parent 0a1036f2c6
commit 4dd2bfaabf
3 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package main
func main() {
}
type SuccessFunc func(string)
type FailFunc func(error)
type ExecuteStringFunc func() (string, error)
type MaybeString struct {
successFunc SuccessFunc
failFunc FailFunc
}
func (s *MaybeString) Success(f SuccessFunc) *MaybeString {
s.successFunc = f
return s
}
func (s *MaybeString) Fail(f FailFunc) *MaybeString {
s.failFunc = f
return s
}
func (s *MaybeString) Execute(f ExecuteStringFunc) {
go func(s *MaybeString) {
str, err := f()
if err != nil {
s.failFunc(err)
} else {
s.successFunc(str)
}
}(s)
}

View File

@ -0,0 +1,39 @@
package main
import "testing"
import "sync"
func TestStringorError_Execute(t *testing.T) {
future := &MaybeString{}
t.Run("Success Result", func(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
future.Success(func(s string) {
t.Log(s)
wg.Done()
}).Fail(func(e error) {
t.Fail()
wg.Done()
})
future.Execute(func() (string, error) {
return "Hello World", nil
})
wg.Wait()
})
t.Run("Failed Result", func(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
future.Success(func(s string) {
t.Fail()
wg.Done()
}).Fail(func(e error) {
t.Log(e.Error())
wg.Done()
})
future.Execute(func() (string, error) {
return "", nil
})
wg.Wait()
})
}

View File

@ -0,0 +1,36 @@
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
// A Future indicates any data that is needed in future but its computation
// can be started in parallel so that it can be fetched from the background when needed.
type data struct {
Body []byte
Error error
}
func futureData(url string) <-chan data {
c := make(chan data, 1)
go func() {
var body []byte
var err error
resp, err := http.Get(url)
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
c <- data{Body: body, Error: err}
}()
return c
}
func main() {
future := futureData("http://test.future.com")
// do many other things
body := <-future
fmt.Printf("response: %#v", string(body.Body))
fmt.Printf("error: %#v", body.Error)
}