Created
May 29, 2022 19:52
-
-
Save aligoren/419b134c56d1bed29af475e89b59aba1 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" | |
"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