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. | |
defer func() { | |
if r := recover(); r != nil { | |
err = fmt.Errorf("panic: %+v", r) | |
} | |
}() | |
// ... the magic ... | |
return result, err |
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. | |
// ... the magic ... | |
return result, err | |
} |
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
result, err := someFunction() // Returns a result or an error | |
if err != nil { | |
// handle 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
{ | |
// A simple student | |
type Student struct { | |
id int | |
grade int | |
name string | |
} | |
// Bind elements to result columns | |
binder := func(s *Student) []any { | |
return []any{&s.id, &s.name, &s.grade} |
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 Query[T any]( | |
db *sql.DB, | |
binder func(*T) []any, | |
query string, | |
args ...any) (error, []*T) { | |
rows, err := db.Query(query, args...) | |
if err != nil { | |
return err, nil | |
} | |
defer func() { |
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 colors(colorNames map[string]tcell.Color) string { | |
var names []string | |
for name := range colorNames { | |
names = append(names, name) | |
} | |
sort.Strings(names) | |
return strings.Join(names, ", ") | |
} | |
func fonts(names []string) string { |
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 readBitmaps(embedFs embed.FS, path string) Font { | |
files, err := embedFs.ReadDir(path) | |
if err != nil { | |
panic(err) | |
} | |
toFontRuneKV := func(f fs.DirEntry) (rune, FontRune) { | |
return toCharName(f.Name()), toFontRune(embedFs, path, f.Name()) | |
} | |
return genfuncs.Associate(files, toFontRuneKV) | |
} |
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 readBitmaps(fs embed.FS, path string) (Font, error) { | |
runes := make(map[rune]FontRune) | |
files, err := fs.ReadDir(path) | |
if err != nil { | |
return nil, err | |
} | |
for _, file := range files { | |
r, err := toFontRune(fs, path, file.Name()) | |
if err != nil { | |
return nil, err |
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
hasData genfuncs.Predicate[string] = func(l string) bool { return len(l) > 2 } | |
toRuneSlice genfuncs.Function[string, []rune] = func(s string) []rune { return []rune(s) } | |
toPixel genfuncs.Function[rune, bool] = func(r rune) bool { return r == blackPixel } | |
toPixelSlice genfuncs.Function[[]rune, []bool] = func(rs []rune) []bool { return genfuncs.Map(rs, toPixel) } |