Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can't send closures over channels in Go.


You can:

  func main() {
  	x := 4
  	f := func() { fmt.Println(x) }
  	ch := make(chan func(), 1)
  	ch <- f
  	f2 := <-ch
  	f2()
	x = 5
	f2()
  }
http://play.golang.org/p/NK5YLVT4zB


  test := make(chan func (int) int)

  go func() {
    f := <- test 
    fmt.Printf("%d\n", f(10))
  }()

  test <- func(v int) int {
    return v * v
  }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: