March 2019
Beginner to intermediate
324 pages
7h 17m
English
In Go, there is a special built-in function called panic. When you invoke panic in your code, your program is interrupted, and a panic message is returned. If a panic gets triggered and you don't capture it in time, your program will stop execution and will exit, so be very careful when you use a panic. Here is a code example:
func panicTest(p bool) { if p { panic("panic requested") }}
In the preceding example, we wrote a function that checks a flag, p. If p is true, then we throw a panic. The argument to the panic function is the message that wants the panic to return. Here is a more complete program that you can run in Go's playground (http://play.golang.org):
package mainimport "fmt"func main() { panicTest(true) ...Read now
Unlock full access