-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
website: https://github.com/golang/go/wiki/cgo broken examples #15937
Comments
One rather ugly workaround is to use a map or an array and pass the func index to C, while it's ugly, it's currently the only way I can think of until this (imho regression) is fixed. /*
extern void go_callback_int(int idx, int p1);
static inline void CallMyFunction(int idx) {
go_callback_int(idx, 5);
}
*/
import "C"
//export go_callback_int
func go_callback_int(idx C.int, p1 C.int) {
foo := myCallbacks[idx].(func(C.int))
foo(p1)
}
func MyCallback(x C.int) {
fmt.Println("callback with", x)
}
var myCallbacks = [...]interface{}{
0: MyCallback,
}
func main() {
C.CallMyFunction(0)
} |
This regression is not going to be fixed. See #12416 and https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md . I'll fix the wiki page. |
I've updated the wiki. |
@inconshreveable can you update an example? This example propose bad hobbits. For example provided solution limit calls of 'register' function by sizeof(int) number. Golang no longer feels like 'google' it feels like 'int', especially after 1.6 release. You have to consider this guys. And a few more ideas about modules and versioning... |
@axet I think you meant to ping @ianlancetaylor |
@axet A better place for discussion would be the golang-dev mailing list, not this issue. See https://golang.org/wiki/Questions. In Go, sizeof(int) is the maximum number of elements you can have in a slice or map. It's not limiting the program. But if you have a better suggestion, I'm happy to adopt it. |
@ianlancetaylor Ok. This is issue about callback example? Anyway. index incremented evey call, not every element. Which means every sizeof(int) no matter how bit it is and how many elements you have (one for example), it will flip over on next sizeof(int) call. Which can happens in few weeks on high load application. That why I said it is issue and bad hobbits. I can suggest create wrapper or support it in Go core. If any one would listen... |
OK, I added a loop. Thanks. Can we talk about this on the mailing list now, please? |
Wiki page contains broken examples in section callback
https://github.com/golang/go/wiki/cgo#function-variables
produces:
The text was updated successfully, but these errors were encountered: