-
-
Save reusee/ba2a6f44fec5d70c81dcc51b6a71e744 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"net/http" | |
"time" | |
) | |
func main() { | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
req, err := http.NewRequestWithContext(ctx, "GET", "http://qq.com", nil) | |
check(err) | |
timer := time.AfterFunc(time.Second*20, func() { | |
cancel() | |
}) | |
timer.Reset(time.Second * 30) // 重设超时 | |
cancel() // 手工取消 | |
resp, err := http.DefaultClient.Do(req) | |
check(err) | |
defer resp.Body.Close() | |
} | |
func check(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment