1. package main
    2. import (
    3. "context"
    4. "fmt"
    5. "time"
    6. )
    7. func main() {
    8. ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    9. defer cancel()
    10. ids := fetchWebData(ctx)
    11. fmt.Println(ids)
    12. }
    13. func fetchWebData(ctx context.Context) (res []int64) {
    14. select {
    15. case <-time.After(3 * time.Second):
    16. return []int64{100, 200, 300}
    17. case <-ctx.Done():
    18. return []int64{1, 2, 3}
    19. }
    20. }