Book Image

Mastering Go

By : Mihalis Tsoukalos
Book Image

Mastering Go

By: Mihalis Tsoukalos

Overview of this book

<p>Often referred to as Golang (albeit wrongly), the Go programming language is really making strides thanks to some masterclass developments, architected by the greatest programming minds. Shopify CEO Tobias Lutke has been recently quoted as saying “Go will be the server language of the future.” Go programmers are in high demand, but - more controversially - Go takes the stage where C and Unix programmers previously led the way.</p> <p>The growth of the Go language has seen it become the means by which systems, networking, web, and cloud applications are implemented. If you’re a Go programmer, you’ll already know some Go syntax and will have written some small projects. However, most Go programmers face the difficulty of having to integrate their Golang skills with production code. With Mastering Go, the author shows you just how to tackle this problem. You'll benefit by mastering the use of the libraries and utilize its features, speed, and efficiency for which the Go ecology is justly famous.</p> <p>Offering a compendium of Go, the book begins with an account of how Go has been implemented. You'll also benefit from an in-depth account of concurrency and systems and network programming imperative for modern-day native cloud development through the course of the book.</p>
Table of Contents (19 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Panic and Recover


This section will present you with a tricky technique that was first mentioned in Chapter 1Go and the Operating System. This technique involves the use of the panic() and recover() functions, and it will be presented in panicRecover.go, which you will review in three parts.

Strictly speaking, panic() is a built-in Go function that terminates the current flow of a Go program and starts panicking! On the other hand, the recover() function, which is also a built-in Go function, allows you to take back the control of a goroutine that just panicked using panic().

The first part of the program follows:

package main

import (
   "fmt"
)

func a() {
   fmt.Println("Inside a()")
   defer func() {
         if c := recover(); c != nil {
               fmt.Println("Recover inside a()!")
         }
   }()
   fmt.Println("About to call b()")
   b()
   fmt.Println("b() exited!")
   fmt.Println("Exiting a()")
}

Apart from the import block, this part includes the implementation of the a(...