Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Panic

This Go code demonstrates the use of the panic function, which is used to terminate the normal execution flow of a program and initiate a panic. Let's go through the code with inline comments and explanations:

// Importing necessary package.
import "os"

// The main function, where the execution of the program begins.
func main() {
    // Initiating a panic with a custom error message.
    panic("a problem")

    // The code below this line will not be executed due to the preceding panic.

    // Attempting to create a file (this code will not be reached).
    _, err := os.Create("/tmp/file")

    // Checking if an error occurred during file creation (this code will not be reached).
    if err != nil {
        // Initiating a panic with the error message if an error is present.
        panic(err)
    }
}

Output

panic: a problem

goroutine 1 [running]:
main.main()
        ..:/...../..../..../panic.go:7 +0x25
exit status 2

Explanation:

  1. panic("a problem"):

    • This line explicitly triggers a panic with the message "a problem."

    • When a panic occurs, the normal flow of the program is halted, and the program terminates abruptly.

  2. _, err := os.Create("/tmp/file"):

    • This line attempts to create a file in the "/tmp" directory.

    • Note that this line and the subsequent lines will not be executed if a panic occurs earlier in the code.

  3. if err != nil { panic(err) }:

    • This code checks if an error occurred during the file creation process (which will not happen in this example due to the preceding panic).

    • If an error is present, it triggers another panic with the error message.

It's important to note that initiating a panic is typically used in exceptional situations where the program cannot continue normal execution. In general, using panics should be done judiciously, and it's often preferable to handle errors in a more controlled manner using error values and other error-handling techniques.

PreviousSorting by FunctionsNextDefer

Last updated 1 year ago

Was this helpful?