August 2019
Beginner to intermediate
798 pages
17h 2m
English
The source code of the second technique will be saved in timeOut2.go, and it will be presented in five parts. This time, the timeout period is provided as a command-line argument to the program.
The first part of timeOut2.go is as follows:
package main
import (
"fmt"
"os"
"strconv"
"sync"
"time"
)
The second code segment of timeOut2.go is shown in the following Go code:
func timeout(w *sync.WaitGroup, t time.Duration) bool {
temp := make(chan int)
go func() {
defer close(temp) time.Sleep(5 * time.Second)
w.Wait()
}()
select {
case <-temp:
return false
case <-time.After(t):
return true
}
}
In the preceding code, the time duration that will be used in the time.After() call is a parameter to the timeout() function, ...
Read now
Unlock full access