Go语言在iPhone上

huangapple go评论173阅读模式
英文:

Go language on iPhone

问题

能否用Go语言编写iPhone应用程序?
我认为需要以下步骤:

  1. 将Go编译为ARM Mach-O二进制文件(我希望GCCGO能够做到这一点)。

  2. 将iPhone应用程序编译为静态库(我认为可以将main()重命名为main2()等)。

  3. 将Go编译为与iPhone静态库链接的Mach-O二进制文件。Go将需要调用main2。

  4. 创建一些plist文件,进行压缩,签名。

英文:

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

答案1

得分: 28

Minux在这里维护Go的iOS移植版本:https://bitbucket.org/minux/goios/wiki/Home

英文:

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

答案2

得分: 6

由于这个问题现在已经有4年了,而且自那时以来发生了很多事情,所以我想要补充已经存在的答案:

这个项目支持使用Go完全编写Android和iOS的本地应用程序(但只能使用有限的API子集),以及使用Android或iOS本地代码通过绑定与Go代码通信的SDK应用程序。

英文:

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.

答案3

得分: 5

能够编译和运行Go程序只是在iPhone上编写Go的一部分。为了能够进行有趣的操作,您仍然需要与iOS API进行绑定。其他人已经提供了将该语言移植到iOS的指导,但在此之后,您还有很长的路要走。

英文:

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.

答案4

得分: 1

对于那些在搜索如何在Go中创建iOS框架或库的人来说,你可以使用cgo将GoLang编译成C库和头文件。

编译器

为了做到这一点,你需要导出一些Go编译器环境变量,并设置CGO_ENABLED=1标志来启用cgo

对于iOS,你需要启用Bitcode(以减小二进制文件大小),并将架构设置为arm64(或amd64用于iOS模拟器),将操作系统设置为darwin。如果你在模拟器上运行,你还需要将SDK设置为iphonesimulator

然后,你需要在go build命令中传递一些参数,比如-buildmode c-archive来生成一个C头文件,以及-trimpath来删除路径信息。

所有这些放在一起,看起来像这样:

$ 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 .

代码

你还需要在你的Go代码中做一些事情。

  1. 你必须导入C模块
import "C"
  1. 你必须通过使用//export functionName注释来导出你想要通过C头文件公开的任何函数。
//export functionName
func functionName() {
  // do something
}
  1. 如果你要传递变量,请确保使用C数据类型,而不是GoLang对象类型。所以string变成了C.CString。例如:
//export helloWorld
func helloWorld() *C.char {
    return C.CString("Hello world")
}

关于这个主题有一篇博文,可以在Compile GoLang as a Mobile Library中找到。

英文:

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

huangapple
  • 本文由 发表于 2012年10月2日 17:49:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/12688065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定