Skip to content

Instantly share code, notes, and snippets.

@killwing
Last active February 27, 2026 10:26
Show Gist options
  • Select an option

  • Save killwing/067510b39b4f629f8b631a17989a409f to your computer and use it in GitHub Desktop.

Select an option

Save killwing/067510b39b4f629f8b631a17989a409f to your computer and use it in GitHub Desktop.
generic utils
package main
import (
"encoding/json"
"fmt"
"os"
)
func bar() (int, error) { return 2, fmt.Errorf("testerr") }
func main() {
must(bar())("bar")
}
func must[T any](ret T, err error) func(s string) T {
return func(s string) T {
if err != nil {
fmt.Printf("%s failed: %s", s, err)
os.Exit(1)
}
return ret
}
}
func deepCopy[T any](de *T) *T {
b, _ := json.Marshal(de)
var ret T
_ = json.Unmarshal(b, &ret)
return &ret
}
func IfThenElse[T any](cond bool, a T, b T) T {
if cond {
return a
}
return b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment