Skip to content

Instantly share code, notes, and snippets.

@Stasenko-Konstantin
Last active November 3, 2023 07:27
Show Gist options
  • Save Stasenko-Konstantin/b45b05a033fd2385a0bbc25686cc7b78 to your computer and use it in GitHub Desktop.
Save Stasenko-Konstantin/b45b05a033fd2385a0bbc25686cc7b78 to your computer and use it in GitHub Desktop.
call/cc in golang
package main
func callCC[A, B, R any](f func(func(A) B) B, res *R) {
defer func() {
if err := recover(); err != nil {
*res = err.(R)
}
}()
f(func(a A) B {
panic(a)
})
}
func main() {
var r = new(int)
f := func(escape func(int) int) int {
return 2 + escape(3)
}
println(f(func(x int) int {
return x
}))
callCC[int, int, int](f, r)
println(*r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment