📄️ Recover a panic
In this example, we use the defer statement to schedule the recovery code to be executed when the function returns. Inside the deferred function, we call recover() to capture the panic value.
📄️ Create a ticker
In this example, we create a new ticker using time.NewTicker, specifying a duration of 1 second. We then start a Goroutine that listens to the ticker.C channel, which will receive a value every time the ticker fires. Inside the Goroutine, we perform the desired action, which in this case is printing a message.
📄️ Loop n seconds
In this example, we specify the number of seconds we want to loop until with the variable n.
📄️ Listen to OS signals (Ctrl+C to exit)
In this example , we create a channel called signalChannel to receive OS signals. We then use signal.Notify to notify the channel when an interrupt (Ctrl+C) or termination signal (SIGTERM) is received.
📄️ Slice output
This example will help to directly see the behavior of various slice operations.
📄️ Check channel is closed
In Go, you can check if a channel is closed or not using a combination of a select statement and an additional boolean variable. Here's an example:
📄️ Directional Channel
In Go, channels can be defined as either unidirectional or bidirectional, allowing you to specify the permitted operations on the channel. This distinction helps enforce the correct usage of channels and provides additional type safety.
📄️ Size Chart
| Data Type | Size (bytes) | Notes |
📄️ Important Concepts
Len vs Cap