MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go Panic function

The panic() function in Go Language is similar to exceptions raised at runtime when an error is encountered. panic() is either raised by the program itself when an unexpected error occurs or the programmer throws the exception on purpose for handling particular errors.

The panic() function is inbuilt in Go Language and when it is raised, the code prints a panic message, and the function crashes. If there are other goroutines ongoing, they function normally and return. Once all goroutines have returned, the program crashes. Below is the prototype of the panic() function:

func panic(v interface{})


Example of Go Panic function

package main

func checkEmployeeAge(name string, age int){
  if age < 18{
    var message string = name + " is underage"
    panic(message)
  }
}

func main() {
  empName := "Jhon"
  age := 14
  checkEmployeeAge(empName, age)  
}

Output

panic: Jhon is underage

goroutine 1 [running]:
main.checkEmployeeAge(...)
/home/PPRwo7/prog.go:6
main.main()
/home/PPRwo7/prog.go:13 +0x86

Conclusion

In this page (written and validated by ) you learned about Go Panic function . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Context.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.