> Go working with C it's very easy and intuitive.. but since i didnt do my
> home work and hack the sources a little more..(its always a pleasure :)) how
> about, how to call Go code from C?
Currently the main function must be written in Go. This could be
changed, and no doubt will be changed in the future, but changing it is
not simple.
You can of course have your Go main call a C function, and do most of
the work in C, and call back to Go where appropriate.
Ian
With deep knowledge and patching some code you can do pretty much
anything you please. :-)
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter
> Is there a way to do it, but its not simple? (can i doit myself, even if it
> requires deep knowledge, and patching some runtime code?) or there' s no way
> at all.
Well, sure, you could do it if you dig into the code deeply enough. The
main issue is that you need to start the overall go runtime, which means
allocating stacks and running all the initializers. The go runtime will
not, of course, return, so if you want to use it in a reasonable way you
would have to have some mechanism by which the call to a Go routine
started the Go runtime in a separate thread.
> second. Can i at least (did'nt try yet) create a shared library? i should
> use go-gcc for that?
Yes, gccgo can create a shared library today. Actually, if you restrict
yourself to gccgo, it would be easier to start up the Go runtime.
> with a shared lib at least would be a possible solution , using dlopen() for
> instance.. not the best way, but at least possible...
Using dlopen doesn't make the real issues any simpler, they're still
there.
Ian
ron
I'm completely lost on why you are trying to do this.
ron
ron
> But are you saying that runtime its in a forever loop? fire-and-forget?
> that would restrict to launch "black boxes" and forget about it in another
> thread..
>
> Or can i initialize the whole world , and start runtime, call some static
> method for instance,
> the method returns, runtime returns and i can pop out some things from
> stack?
> (even if you has to do some assembly (ich) for that?!)
I suppose in principle one could arrange for the runtime to return if
the function return and there were no goroutines running. That would
tend to be error-prone, though, as it's very easy to have a goroutine
sitting around waiting for something to happen, e.g., if you use a
timer.
Ian
> I think its a comfort idea to think that if i need more agressive
> optimization , i can write C code (whatever i want in the software cycle)
> synce Go is aimed most at system programming.. its kind like assembly is to
> C.. we dont used to get into that level often.. but if you need, more
> optimization you can get it..
Let's be clear, though: Go code can call C code just fine. And Go code
can call C code and that C code can call back into Go code. What does
not work at present is having a program whose main function is in C
which then wants to call Go code. That is, you can not embed Go code in
a C program.
I expect this will be fixed at some point--sooner if somebody starts
working on it.
Ian