Created
July 22, 2023 03:15
-
-
Save ostretsov/e3b4e0ab5dc79de7d5a1363ef90a7293 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"fmt" | |
"time" | |
) | |
func Example_fnDoesntRespectContext() { | |
fn := func(ctx context.Context) { | |
time.Sleep(100 * time.Millisecond) | |
fmt.Println("Плевать на контекст, я все равно работаю") | |
} | |
wait := make(chan struct{}) | |
ctx, cancel := context.WithCancel(context.Background()) | |
go func() { | |
fn(ctx) | |
close(wait) // signalling the function has finished | |
}() | |
cancel() // cancel does not stop the function | |
<-wait | |
// Output: Плевать на контекст, я все равно работаю | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment