See Android go! Go, Android. Go!

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Using the Gomobile tool chain and go 1.5 you can compile go code to work on Android and IOS. Can either write your application completely in go with OpenGL to your UI, or write a go library that can interact WI Th native Android/ios. This opens up the doors for cross platform libraries (business logic anyone?), which have me pretty excited. Google has even do some of the legwork to make sure that apps written on go aren ' t rejected from the app store with Ivy.

In this post I'll go over the very basic setup to compile and install a Go app on Android, and briefly explain what ' s go ing on.

Getting started

Let's get started, first need to has a working go 1.5 install. You can compile it from source or use the pre compiled version. I used go 1.5 beta2 while writing this blog post to a Linux machine. I noticed some warnings in the "Go Mobile source" about things not working in Windows so if is using Windows for Develo Pment.

Once You has a working go install, you need to install the Gomobile command by running:

 go get golang.org/x/mobile/cmd/gomobile gomobile init

For android:you need to install the Android SDK and ensure that ADB are on your path and that USB debugging are enabled on Your device.

For ios:unfortunately IOS support isn ' t 100% the ready for prime time and is not the work. As usual to compile for iOS I-still need to has Xcode installed and is working on OSX.

Installing an example

Let's see if we can compile and install some go code. Google have provided some examples we can use. For the sake of simplicity I ' m is only going to talk about installing to Android from here. (mostly because I don ' t have a iOS device to try)

The following commands would install the sample applications

 gomobile install golang.org/x/mobile/example/basic gomobile install golang.org/x/mobile/example/audio gomobile install golang.org/x/mobile/example/sprite

Might not seem like much, but I thought it is pretty cool. The above applications is pure go and is using OpenGL for display. Currently the APIs is somewhat restricted but I expect this would improve as go matures.

Anatomy of cross platform apps

OK so we can compile other people's code, but what's it doing? Let's take a look at the "s happening in the basic example.

//excerpt from Golang.org/x/mobile/example/basicfunc Main() {    app.Main(func(a app.App) {        var C Config.Event         for e := Range a.Events() {            Switch e := app.Filter(e). (type) {             Case Lifecycle.Event:                Switch e.crosses(Lifecycle.stagevisible) {                 Case Lifecycle.Crosson:                    OnStart()                 Case Lifecycle.Crossoff:                    OnStop()                }             Case Config.Event:                C = e                Touchloc = Geom. Point{C.Width / 2, C.Height / 2}             Case Paint.Event:                OnPaint(C)                a.EndPaint()             Case Touch.Event:                Touchloc = e.Loc            }        }    })}

Apps written in Go is expected to the Main function from the app package. From there your can define what certain events should does, check the event documentation for in depth details. These events fire based on registered event interface{} in the app.

The above code is looping through all events, come in on the event channel. The config event defines the size of the screen, the paint event was cycling the colour of our triangle. The touch event changes the position of the triangle, and the lifecycle event constructs or destructs the program based on Application focus.

Further reading

I hope I had piqued your interest, to take go mobile further check out these links

All the examples can is found Http://golang.org/x/mobile/example
documentation can be found Https://godoc.org/gola Ng.org/x/mobile
Source can be found Https://github.com/golang/mobile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.