如何在Android上运行我的Go代码?

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

How to run my Go code in Android?

问题

如何在我的Android设备上运行这段Go代码?(在Fedora 15上可以运行)。

package main
import "fmt"

func main() {
    fmt.Println("Hello, 世界")
}
英文:

How can i run this Go code in my Android? (in Fedora 15 its working).

package main
import "fmt"

func main() {
	fmt.Println("Hello, 世界")
}

答案1

得分: 11

你必须为ARM编译它,幸运的是,使用Go的编译器非常容易:

$ 5g main.go && 5l main.5

执行二进制文件(5.out)将可以在Android上运行。只需将其复制到那里并在shell中运行。更多信息在这里

英文:

You have to compile it for ARM, and thankfully it is very easy with Go's compilers:

$ 5g main.go && 5l main.5

The executive binary (5.out) will be runnable on Android. Just copy it there and run with shell. More info here.

答案2

得分: 3

使用Go 1.0版本后,编译器从为不同目标架构提供单独的编译器可执行文件变为一个单一的编译器可执行文件。因此,与Go 1.0版本相比,编译ARM的过程略有不同:

CGO_ENABLED=0
GOOS=linux
GOARCH=arm
go build main.go

需要设置环境变量GOOS和GOARCH以匹配Android环境,即Linux操作系统和ARM硬件架构。然后,您可以像为任何其他平台编译一样使用go build(它将根据设置的变量进行操作)。

英文:

With Go 1.0, the compilers changed from separate compiler executables for different target architectures to a single compiler executable. Thus, the process of compiling for ARM is slightly different from Go 1.0 on:

CGO_ENABLED=0
GOOS=linux
GOARCH=arm
go build main.go

The (environment) variables GOOS and GOARCH have to be set to match the android environment, which is a linux OS and an ARM hardware architecture. Then you can use go build as you would compile for any other platform (which will then act according to the set variables).

huangapple
  • 本文由 发表于 2011年12月27日 18:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/8643741.html
匿名

发表评论

匿名网友

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

确定