Search code examples
go

What is the Go equivalent to assert() in C++?


I'm looking for a condition check in Go which can terminate the program execution like assert in C++.


Solution

  • As mentioned by commenters, Go does not have assertions.

    A comparable alternative in Go is the built-in function panic(...), gated by a condition:

    if condition {
      panic(err)
    }
    

    This article titled "Defer, Panic, and Recover" may also be informative.