Book Image

Hands-On Go Programming

By : Tarik Guney
Book Image

Hands-On Go Programming

By: Tarik Guney

Overview of this book

<p>With its C-like speed, simplicity, and power for a growing number of system-level programming domains, Go has become increasingly popular among programmers. Hands-On Go Programming teaches you the Go programming by solving commonly faced problems with the help of recipes. You will start by installing Go binaries and get familiar with the tools used for developing an application. Once you have understood these tasks, you will be able to manipulate strings and use them in built-in function constructs to create a complex value from two floating-point values. You will discover how to perform an arithmetic operation date and time, along with parsing them from string values. In addition to this, you will cover concurrency in Go, performing various web programming tasks, implementing system programming, reading and writing files, and honing many fundamental Go programming skills such as proper error handling and logging, among others. Whether you are an expert programmer or newbie, this book helps you understand how various answers are programmed in the Go language.</p>
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributor
Preface
Index

Gracefully dealing with panics


In this section, we're going to see how to gracefully deal with panics. Unlike with errors, if you don't recover from panics, it will stop the execution of your program. Therefore, dealing with them is important if you want your program to continue. First of all, let's see how we can throw a panic in our Go program. You can simply use a keyword called panic, which is a built-in function, type panicked, and run it to obtain the output:

 

 

There's another way to do this. Let's use another function here and write something. Let's imagine we're doing something and for some reason it just panicked. This might be a third-party method, which means it is located in a third-party package, so we may not have full control over that package. So, here, if you run the preceding code, this is what we're going to see in the application window, along with the message we want to write to the console, which is as follows:

We also see the stack trace of our panic here. First, it...