23

Is it possible to write in Go for iPhone? I think following steps are required

  1. Compile Go as ARM Mach-O binary (I expect GCCGO be able to do that)

  2. Compile iPhone app as static library (I think it possible to rename main() -> main2(), etc)

  3. Compile Go as Mach-O binary linked with iPhone static library. Go will have to call main2.

  4. make some plist files, zip, sign

Max
  • 6,286
  • 5
  • 44
  • 86

4 Answers4

28

Minux maintains the Go iOS port here: https://bitbucket.org/minux/goios/wiki/Home

zzzz
  • 87,403
  • 16
  • 175
  • 139
  • This article seems to be a good starting point for the almost not documented minux-project : https://medium.com/using-go-in-mobile-apps (especially parts 2-4). – Christoph Sonntag Jan 31 '15 at 11:47
6

Since this question is 4 years old now and a lot has happened since then I thought I'd add to the already existing answers:

This github project supports writing native applications entirely written in Go for Android and iOS (with a limited subset of APIs available, however) and SDK applications where the Android or iOS native code communicates with the Go code via bindings.

TheBaj
  • 1,091
  • 1
  • 10
  • 22
5

Being able to compile and a go program is only part of writing go for the iPhone. You will still need bindings to the iOS API's in order to do anything interesting with it. Others have already given pointers to ports of the language to iOS but you'll still have a long way go after that.

Jeremy Wall
  • 23,907
  • 5
  • 55
  • 73
  • 1
    My scenario was to make 2 apps. Go app and iOS app and link them together. They will communicate with some API. Go app will simulate world and produce timed events. iOS app will work with graphics and sensors. – Max Oct 02 '12 at 19:25
  • That might work. Assuming the go app doesn't have to access files or datastorage. – Jeremy Wall Oct 02 '12 at 20:14
  • No. It was great to simulate game logic in Go. e.g. Play sound after 300ms some event if something else have not happened. We modeled logic in Go and wish to use logic directly in app. – Max Oct 02 '12 at 20:31
  • @Max Can you share your experience with using Go lang on iOS? – Nik Jun 17 '14 at 17:14
  • According to the Clean architecture, you might live well without need for iOS or Android APIs if you'll write all your business logic (use cases/interactors, domain logic) in Go and application-specific logic in Objective-C/Swift and Java respectively. – Dmitry Zaytsev Jan 06 '16 at 10:51
1

For people who stumble upon this while searching for how to create an iOS framework or library in Go, you can use cgo to compile GoLang into a c library and header file.

Compiler

To do this, you have to export some Go-compiler environment variables and set the CGO_ENABLED=1 flag to enable cgo.

For iOS, you have to enable Bitcode (which reduces binary size), and set the architecture to be arm64 (or amd64 for iOS Simulator), and set the OS as darwin. If you are running on the Simulator, you'll also want to set your SDK to iphonesimulator.

Then you pass some parameters to your go build command to say -buildmode c-archive to generate a C header file and -trimpath to remove the path information.

All together, that looks like this:

$ export SDK=iphonesimulator
$ export GOOS=darwin
$ export GOARCH=arm64
$ export CGO_ENABLED=1
$ export CGO_CFLAGS="-fembed-bitcode"
$ go build -buildmode c-archive -trimpath -o outputFilename.a .

Code

You'll also have to do a couple things in your Go code.

  1. You must import the "C" module
import "C"
  1. You must export any functions you want to expose through the C header file by decorating these functions with the //export functionName comment.
//export functionName
func functionName() {
  // do something
}
  1. If you are passing variables around, make sure you use the C data types, not the GoLang object types for these variables. So string becomes C.CString. For example:
//export helloWorld
func helloWorld() *C.char {
    return C.CString("Hello world")
}

There's a blog article about this topic at Compile GoLang as a Mobile Library

Adonis Gaitatzis
  • 3,189
  • 26
  • 24
  • Following your blog, I'm unable to get this to work. `sayHello()` is unknown. Using the latest Xcode, iOS, etc... – marcantonio Oct 12 '22 at 23:29