Skip to content

Instantly share code, notes, and snippets.

@tidwall
Created March 2, 2026 00:55
Show Gist options
  • Select an option

  • Save tidwall/c819f579fb6febec4ff5414013745afc to your computer and use it in GitHub Desktop.

Select an option

Save tidwall/c819f579fb6febec4ff5414013745afc to your computer and use it in GitHub Desktop.
C-like assert for Go. Snippet.
// Tiny C-like assert. For a robust assert: github.com/tidwall/assert.
func assert(cond bool) {
if !cond {
_, path, ln, _ := runtime.Caller(1)
data, _ := os.ReadFile(path)
fmt.Fprintf(os.Stderr, "Assertion failed: %s, file %s, line %d.\n",
"("+strings.SplitN(strings.TrimSpace(strings.Split(string(data),
"\n")[ln-1]), "(", 2)[1], filepath.Base(path), ln)
os.Exit(6)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment