Book Image

Go Systems Programming

Book Image

Go Systems Programming

Overview of this book

Go is the new systems programming language for Linux and Unix systems. It is also the language in which some of the most prominent cloud-level systems have been written, such as Docker. Where C programmers used to rule, Go programmers are in demand to write highly optimized systems programming code. Created by some of the original designers of C and Unix, Go expands the systems programmers toolkit and adds a mature, clear programming language. Traditional system applications become easier to write since pointers are not relevant and garbage collection has taken away the most problematic area for low-level systems code: memory management. This book opens up the world of high-performance Unix system applications to the beginning Go programmer. It does not get stuck on single systems or even system types, but tries to expand the original teachings from Unix system level programming to all types of servers, the cloud, and the web.
Table of Contents (13 chapters)

About timeouts

Can you imagine waiting forever for something to perform an action? Neither can I! So, in this section you will learn how to implement timeouts in Go with the help of the select statement.

The program with the sample code will be named timeOuts.go and will be presented in four parts; the first part is the following:

package main 
 
import ( 
   "fmt" 
   "time" 
) 

The second part of timeOuts.go is the following:

func main() { 
   c1 := make(chan string) 
   go func() { 
         time.Sleep(time.Second * 3) 
         c1 <- "c1 OK" 
   }() 

The time.Sleep() statement in the goroutine is used for simulating the time it will take for the goroutine to do its real job.

The third part of timeOuts.go has the following code:

   select { 
   case res := <-c1: 
         fmt.Println(res) 
   case <-time.After(time.Second * 1): 
     ...