This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
printV := func(values ...int) { fmt.Println(values) } | |
args := []int{1, 2, 3, 4} | |
printv(args...) | |
// output: [1 2 3 4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fmt.Println(rotate(rotate(1, 2, 3))) | |
// output: 3 1 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aa, bb, cc = bb, cc, aa | |
fmt.Println(aa, bb, cc) | |
// output: 3 1 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rotate := func(a, b, c int) (int, int, int) { | |
return b, c, a | |
} | |
aa, bb, cc := rotate(1, 2, 3) | |
fmt.Println(aa, bb, cc) | |
// output: 2 3 1 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func foo() *Result[int] {...} | |
func fooBar() *Result[string] { | |
return Map(foo(), func(i int) *Result[string] { | |
return NewResult(fmt.Print(i)) | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Result[T any] struct { | |
//... | |
} | |
// NewResult for a value. | |
func NewResult[T any](t T) *Result[T] {...} | |
// NewResultError creates a Result from a value, error tuple. | |
func NewResultError[T any](t T, err error) *Result[T] {...} | |
// NewError for an error. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This returns a single error and mutates v! | |
func json.Unmarshal(data []byte, v any) error {} | |
// This mutates b and returns an int and an error. | |
func (f *File) os.ReadAt(b []byte, off int64) (n int, err error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func foo() (int, error) {...} | |
func fooBar() (string, error) { | |
value, err := foo() | |
if err != nil { | |
return 0, err | |
} | |
// ... | |
return fmt.Print(value), nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// All returns true if all elements in the sequence match the predicate. | |
func All[T any](sequence Sequence[T], predicate func(t T) bool) bool {...} | |
// Any returns true if any element of the sequence matches the predicate. | |
func Any[T any](sequence Sequence[T], predicate func(t T) bool) bool {...} | |
// Compare two sequences with a comparator returning less/equal/greater (-1/0/1) and return comparison of the two. | |
func Compare[T any](s1, s2 Sequence[T], comparator func(t1, t2 T) int) int {...} | |
// Find returns the first element matching the given predicate, or Result error of NoSuchElement if not found. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func CaculateSomething(i int) (result int, err error) { | |
// Does a bunch of math to calculate a value, or | |
// returns an error when it can't. | |
done := false | |
defer func() { | |
if !done { | |
err = fmt.Errorf("panic: %+v", recover()) | |
} | |
}() | |
// ... the magic ... |
NewerOlder