Created
March 2, 2026 00:55
-
-
Save tidwall/c819f579fb6febec4ff5414013745afc to your computer and use it in GitHub Desktop.
C-like assert for Go. Snippet.
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
| // 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