Skip to content

Instantly share code, notes, and snippets.

@reusee
Created June 22, 2020 12:56
Show Gist options
  • Save reusee/ba2a6f44fec5d70c81dcc51b6a71e744 to your computer and use it in GitHub Desktop.
Save reusee/ba2a6f44fec5d70c81dcc51b6a71e744 to your computer and use it in GitHub Desktop.
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