Last active
March 10, 2016 01:40
-
-
Save abraithwaite/d35ee0d124fb0821a6ca to your computer and use it in GitHub Desktop.
Golang "Canceling" (Useless without something like net.Context)
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 ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
c := make(chan string, 1) | |
go func() { c <- mysleep() }() | |
select { | |
case <-c: | |
fmt.Println("returned") | |
case <-time.After(time.Second * 1): | |
fmt.Println("timeout") | |
} | |
time.Sleep(time.Second * 20) | |
} | |
func mysleep() string { | |
defer fmt.Println("defer") | |
fmt.Println("enter") | |
time.Sleep(time.Second * 20) | |
defer fmt.Println("exit") | |
return "return" | |
} |
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
17:37:40 $ go run /tmp/test.go | |
enter | |
timeout | |
SIGQUIT: quit | |
PC=0x459213 m=0 | |
goroutine 6 [syscall]: | |
runtime.notetsleepg(0x58b798, 0x46c75ab6f, 0x0) | |
/usr/local/go/src/runtime/lock_futex.go:205 +0x4e fp=0xc820028738 sp=0xc820028710 | |
runtime.timerproc() | |
/usr/local/go/src/runtime/time.go:209 +0xde fp=0xc8200287c0 sp=0xc820028738 | |
runtime.goexit() | |
/usr/local/go/src/runtime/asm_amd64.s:1998 +0x1 fp=0xc8200287c8 sp=0xc8200287c0 | |
created by runtime.addtimerLocked | |
/usr/local/go/src/runtime/time.go:116 +0x11f | |
goroutine 1 [sleep]: | |
time.Sleep(0x4a817c800) | |
/usr/local/go/src/runtime/time.go:59 +0xf9 | |
main.main() | |
/tmp/test.go:17 +0x1d0 | |
goroutine 5 [sleep]: | |
time.Sleep(0x4a817c800) | |
/usr/local/go/src/runtime/time.go:59 +0xf9 | |
main.mysleep(0x0, 0x0) | |
/tmp/test.go:23 +0x20f | |
main.main.func1(0xc82006c060) | |
/tmp/test.go:10 +0x18 | |
created by main.main | |
/tmp/test.go:10 +0x74 | |
rax 0xfffffffffffffffc | |
rbx 0xc8200286c8 | |
rcx 0xffffffffffffffff | |
rdx 0x0 | |
rdi 0x58b798 | |
rsi 0x0 | |
rbp 0x3b93776f | |
rsp 0xc820028690 | |
r8 0x0 | |
r9 0x0 | |
r10 0xc8200286c8 | |
r11 0x206 | |
r12 0x1 | |
r13 0x52ef52 | |
r14 0x9 | |
r15 0x8 | |
rip 0x459213 | |
rflags 0x206 | |
cs 0x33 | |
fs 0x0 | |
gs 0x0 | |
exit status 2 | |
17:37:44 $ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment