Skip to content

Instantly share code, notes, and snippets.

@aligoren
Created May 29, 2022 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aligoren/419b134c56d1bed29af475e89b59aba1 to your computer and use it in GitHub Desktop.
Save aligoren/419b134c56d1bed29af475e89b59aba1 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"errors"
"fmt"
"time"
)
func AmirimOlayYerine(ctx context.Context) error {
time.Sleep(500 * time.Millisecond)
return errors.New("Amirimin aracı kaza yaptı")
}
func DinlemeYapiliyor(ctx context.Context) {
fmt.Println("Çok ağır bir yük geliyor ama hadi hayırlısı...")
select {
case <-time.After(2 * time.Second):
fmt.Println("Oh be sonunda bunu tamamladık ya")
case <-ctx.Done():
fmt.Println("Lan noluyor? Bu işlem bir anda durduruldu. Anlayamadım")
}
}
func main() {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
go func() {
AmirimOlayYerine(ctx)
}()
DinlemeYapiliyor(ctx)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment