6

Is it possible to use Go to build Android games at all? I'm not wedded to the technologies mentioned in the subject line. I know that some people have built some Android programs in Go, but they may have been headless.

Curyous
  • 8,716
  • 15
  • 58
  • 83

2 Answers2

1

No, it is not possible right now. Go and C programs can interoperate via cgo. but in that case, the Go program has to start and initialize its runtime before passing control to the C-based part of the program.

When using the NDK in Android, your C code is called by the Dalvik virtual machine via dlopen. So the Go runtime would not have a chance to initialize itself.

Jeff Allen
  • 1,391
  • 13
  • 19
  • Couldn't a call be made from the C code to Go to do initialisation? – kristianp Sep 05 '12 at 06:37
  • Yes, in principle... like "pigs can fly if they have enough kinetic energy". But the way Go and cgo work today, no. A fundamental limit of the current implementation is that the ELF header of the executable has to be made by the Go compiler and has to point at Go's init code. Once that's done, then Go can allocate a C-style stack and transition onto and off of it when calling into C code. – Jeff Allen Sep 05 '12 at 07:21
-1

If you want non-headless apps, my advice would be to use cgo for the GUI. That sounds counterintuitive, but if the NDK supports C android gui libraries, it'd probably be easiest to write the GUI using those calls. Of course, you don't have to write all the logic in C. You could simply cgo wrappers for each of the GUI calls and then write the GUI in go, except that each gui call would be translated through cgo.

joshlf
  • 21,822
  • 11
  • 69
  • 96
  • Go/cgo/Java can't interoperate like this. – Jeff Allen Aug 28 '12 at 14:36
  • I'm not suggesting use of Java. NDK supports native C support on android devices - there's no Java involved. My point is that if you could write the android system-interface bits in C using NDK, you could then link using cgo – joshlf Sep 08 '12 at 19:15
  • My understanding of the application lifecycle is that the process for your app is a forked copy of the dalvik zygote, and that once your uid/gid is set right, there's a transfer of control from Android to your app, via the Java calling conventions. If the very first thing you want to do is hand over control to C code compiled by the NDK, fine. But Go's runtime wants to initialize itself in the absence of Dalvik already running in the address space. – Jeff Allen Sep 11 '12 at 13:12