You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i think we discussed arrow function previous, and discussed error check again and again
but why we are still discussing it, maybe we didn't get the sweet point
hope make a little progress
below is common code, just do somethings, f1, f2, f3( here makePeople)...
OCZ, we need check every step's err result to progress next
i add a func Dos to collect err check, it works(sample1),
but i find if Go support arrow function, we can make it more simple(sample2)
package main
import (
"fmt"
)
type people struct {
age int
name string
}
var limit int
func f1() error {
//blabla for init something
if limit > 1000 {
return fmt.Errorf("reach limit")
}
return nil
}
func f2(age int) (int, error) {
if age < 0 {
return 0, fmt.Errorf("invalid age")
}
return age, nil
}
func makePeople(name string, age int) (*people, error) {
if len(name) == 0 {
return nil, fmt.Errorf("invalid name")
}
return &people{age, name}, nil
}
type Action func() error
func Dos(actions []Action) error {
for i := range actions {
err := actions[i]()
if err != nil {
return err
}
}
return nil
}
func sample1(ageIn int) {
var (
err error
age int //middle result
pp *people //final result
)
err = Dos(
[]Action{
func() error { //here is a little boring, can we make it more simple ?
return f1()
},
func() error { //can we omit the word func and error, to deduce it ?
age, err = f2(ageIn)
return err
},
func() error {
pp, err = makePeople("Alice", age)
return err
},
})
if err == nil {
fmt.Println("here we got people:", *pp)
} else {
fmt.Println("we failed:", err)
}
}
/*
//currently, Go does support ()=>
//
func sample2(ageIn int) {
var (
err error
age int //middle result
pp *people //final result
)
err = Dos([]Action{
()=> f1(), //f1 match Action
()=> {
age, err = f2(ageIn) //here we save age, return err only to match Action
return err
},
()=> {
pp, err = makePeople("Alice", age) //same with above
return err
},
})
if err == nil {
fmt.Println("here we got people:", *pp)
} else {
fmt.Println("we failed:", err)
}
}
*/
func main() {
sample1(20)
sample1(-1)
limit = 1005
sample1(20)
}
The text was updated successfully, but these errors were encountered:
Proposal Details
i think we discussed arrow function previous, and discussed error check again and again
but why we are still discussing it, maybe we didn't get the sweet point
hope make a little progress
below is common code, just do somethings, f1, f2, f3( here makePeople)...
OCZ, we need check every step's err result to progress next
i add a func Dos to collect err check, it works(sample1),
but i find if Go support arrow function, we can make it more simple(sample2)
The text was updated successfully, but these errors were encountered: