Calling methods written in Go from a Java or C program

1,853 views
Skip to first unread message

Alexandre Bilger

unread,
Jul 23, 2010, 9:07:23 AM7/23/10
to golang-nuts
do you think it will be possible in the long run
to load a go written "module/library" into another language like java
or c ?

Ian Lance Taylor

unread,
Jul 23, 2010, 9:27:33 AM7/23/10
to Alexandre Bilger, golang-nuts
Alexandre Bilger <abi...@gmail.com> writes:

You can already do this from C--provided that your main function is
written in Go.

If you want to have a program whose main function is written in C call
subroutines written in Go, that doesn't work today, but it is possible
to make it work. This is not something the Go team is likely to work
on, though. If somebody else wants to work on it, you would basically
have to have to link in the Go runtime library, and have it start up the
first time a Go function was called. You would have to do something to
make sure that the initializers were run for all imported Go packages.

Any direct interaction between Go and Java is almost surely going to be
via C, one way or another.

In general you may want to consider communicating over a socket.

Ian

Alexander Surma

unread,
Jul 23, 2010, 9:31:46 AM7/23/10
to golan...@googlegroups.com

> In general you may want to consider communicating over a socket.
Which in my opinion is *the* answer. That keeps things modular and even
independent from the location of execution.
(This answer is also valid for the always-recurring "Go and dynamic
linking?" threads)

Surma

chris dollin

unread,
Jul 23, 2010, 9:40:50 AM7/23/10
to Ian Lance Taylor, Alexandre Bilger, golang-nuts
On 23 July 2010 14:27, Ian Lance Taylor <ia...@google.com> wrote:

> Alexandre Bilger <abi...@gmail.com> writes:
> Any direct interaction between Go and Java is almost surely going to be
> via C, one way or another.

How mad would someone have to be to consider compiling Go into
JVM code?

(Not me, I have enough mad projects already.)

Chris

--
Chris "allusive" Dollin

Evan Shaw

unread,
Jul 23, 2010, 9:52:05 AM7/23/10
to chris dollin, golang-nuts
On Fri, Jul 23, 2010 at 8:40 AM, chris dollin <ehog....@googlemail.com> wrote:
> On 23 July 2010 14:27, Ian Lance Taylor <ia...@google.com> wrote:
>> Alexandre Bilger <abi...@gmail.com> writes:
>> Any direct interaction between Go and Java is almost surely going to be
>> via C, one way or another.
>
> How mad would someone have to be to consider compiling Go into
> JVM code?

I think it's more likely that Go code would be called through the JNI
somehow. Compiling Go to JVM bytecode definitely involves a number of
difficulties with relatively few benefits.

- Evan

Alexandre Bilger

unread,
Jul 23, 2010, 9:56:13 AM7/23/10
to golang-nuts
i was thinking of something like hosting the go runtime in c and
calling it from java through JNI
this kind of functionality would permit migrating large codebase from
one language to go incrementaly over time

thanks for your answers

Alex

On Jul 23, 3:52 pm, Evan Shaw <chicken...@gmail.com> wrote:
> On Fri, Jul 23, 2010 at 8:40 AM, chris dollin <ehog.he...@googlemail.com> wrote:
> > On 23 July 2010 14:27, Ian Lance Taylor <i...@google.com> wrote:

David Klaffenbach

unread,
Jul 23, 2010, 10:32:29 PM7/23/10
to golang-nuts
How about the opposite? Calling .dll, .so or similar libraries from
go.

Is there something comparable to python's ctypes?

Ian Lance Taylor

unread,
Jul 24, 2010, 12:18:10 PM7/24/10
to David Klaffenbach, golang-nuts
David Klaffenbach <dkl...@gmail.com> writes:

cgo.

Or SWIG.

Ian

David Klaffenbach

unread,
Jul 24, 2010, 7:03:26 PM7/24/10
to golang-nuts
Thanks Ian. I'd missed cgo's existence, and it appears swig is on the
roadmap at least.

I've been using cytpes in python recently for accessing functions in
dlls on windows and was wondering how I'd do the same with go.

jimt

unread,
Jul 24, 2010, 7:09:14 PM7/24/10
to golang-nuts
Most of the projects listed here (http://go-lang.cat-v.org/library-
bindings) interact with shared c-libraries using Cgo. It'll probably
be easiest to just have a look at the source for some of them.

brainman

unread,
Jul 24, 2010, 8:14:24 PM7/24/10
to golang-nuts
On Jul 25, 9:03 am, David Klaffenbach <dkl...@gmail.com> wrote:

> I've been using cytpes in python recently for accessing functions in
> dlls on windows and was wondering how I'd do the same with go.

There is an example of calling windows dll inside comment in
http://golang.org/src/pkg/syscall/syscall_windows.go:

package main

import (
"syscall"
)

func abort(funcname string, err int) {
panic(funcname + " failed: " + syscall.Errstr(err))
}

func print_version(v uint32) {
major := byte(v)
minor := uint8(v >> 8)
build := uint16(v >> 16)
print("windows version ", major, ".", minor, " (Build ", build, ")
\n")
}

func main() {
h, err := syscall.LoadLibrary("kernel32.dll")
if err != 0 {
abort("LoadLibrary", err)
}
defer syscall.FreeLibrary(h)
proc, err := syscall.GetProcAddress(h, "GetVersion")
if err != 0 {
abort("GetProcAddress", err)
}
r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0)
print_version(uint32(r))
}

I haven't tried building it, but it looks ok to me.


Alex

Cory Mainwaring

unread,
Jul 24, 2010, 9:29:47 PM7/24/10
to brainman, golang-nuts
It builds.

Ian Lance Taylor

unread,
Jul 25, 2010, 3:05:50 PM7/25/10
to David Klaffenbach, golang-nuts
David Klaffenbach <dkl...@gmail.com> writes:

> Thanks Ian. I'd missed cgo's existence, and it appears swig is on the
> roadmap at least.

SWIG is working and the patches have all been contributed back to the
SWIG sources. There has not been a SWIG release since the patches were
contributed, though.

Ian

David/Jones

unread,
Jul 26, 2010, 8:52:09 AM7/26/10
to golang-nuts
Why SWIG?
Why not some awesome IPC?
I really think IPC is the FFI of the future.
It surely would require more thinking, though...
Reply all
Reply to author
Forward
0 new messages