September 2017
Intermediate to advanced
466 pages
9h 33m
English
In this subsection, you will learn how to set a timeout for a http.Get() request. For reasons of simplicity, it will be based on the Go code of getURL.go. The name of the program will be timeoutHTTP.go and will be presented in five parts.
The first part of the program is the following:
package main import ( "fmt" "io" "net" "net/http" "os" "path/filepath" "time" ) var timeout = time.Duration(time.Second)
Here, you declare the desired timeout period, which is 1 second, as a global parameter.
The second part of timeoutHTTP.go has the following Go code:
func Timeout(network, host string) (net.Conn, error) { conn, err := net.DialTimeout(network, host, timeout) if err != nil { return nil, err } conn.SetDeadline(time.Now().Add(timeout)) ...Read now
Unlock full access