GoMobile is currently an experimental project at Google and is not yet available to users. Support ios and Android dual platforms, support all gO apps (All-Go App), (All-Go app currently only supports OPEN GL drawing interface), support Java, Object-C interoperability (Go Libraries).

Making: github.com/golang/mobi…

Wiki: github.com/golang/go/w…

Gobind: godoc.org/golang.org/…

GoMobile configuration

The Go environment has been configured by default.

Automatic installation

$ go get golang.org/x/mobile/cmd/gomobileCopy the code

Pit: need scientific Internet access, may verify certificate failure, the author failed to download in the company network.

Manual installation

download

Change the file name to mobile and copy it to $GOPATH/ SRC /golang.org/x/

$ git clone https://github.com/golang/mobileCopy the code

build

$GOPATH/bin creates the gomobile executable program. Copy the gomobile executable program to /usr/local/go/bin/. Run the gomobile command.

$ go build golang.org/x/mobile/cmd/gomobileCopy the code

init

After the environment is initialized, the system automatically downloads and installs dependencies, and you need to climb over the wall. A message may be displayed indicating that the permission is denied. Add the corresponding file permission.

$ gomobile initCopy the code

config ndk

The error “No Android NDK path is set” may be displayed. The solutions are as follows:

Because gomobile init does not download the NDK at the same time, you need to manually download the NDK environment and run it

$ gomobile init -ndk ~/Library/Android/sdk/ndk-bundle/Copy the code

The last path is the NDK installation path, which is selected for your own installation directory.

All-GO APP

Google demo, source directory: golang.org/x/mobile/example/basic

Compile the Android Demo

$ gomobile build -target=android golang.org/x/mobile/example/basicCopy the code

Run Android Demo

Verify that ADB has identified the phone

$ gomobile install golang.org/x/mobile/example/basicCopy the code

Once installed successfully, you can open a triangle program that moves with gestures.

Go Libraries

Google demo, source directory: golang.org/x/mobile/example/bind/

Android directory for Android Studio Project, Hello directory for go Libs source code

Go libs to create

GOPTAH created in libs directory to store the go source [corresponding golang.org/x/mobile/example/bind/hello] in the demo

// Network request, enter url, Responsepackage helloImport (" FMT ""net/ HTTP" "IO /ioutil")func Greetings(URL string) string {resp, err := http.Get(url) if err ! = nil { return fmt.Sprintf("error: %s", err) } b, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err ! = nil { return fmt.Sprintf("error: %s", err) } return fmt.Sprintf("Hello, %s!" , b)}Copy the code

Android configuration

Libs Module

Create the libs module golang.org/x/mobile/example/bind/android/hello] [corresponding to the demo, app module for its dependencies

Libs Module builds go source dependencies, and the build file configuration is as follows

Plugins {id "org.golang.mobile. Bind "version "0.2.6"}gobind {/* go libs directory */ PKG = "Golang.org/x/mobile/example/bind/hello" / * * / gopath gopath directory = "/ Users/hangsong/go / * * / go = gobin directory "/ usr/local/go/bin" / * * / GOMOBILE = gombile directory "/ usr/local/go/bin/GOMOBILE"}Copy the code

App Module

After build is complete, call the Go libs code in MainActivity

package org.golang.example.bind; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import hello.Hello; public class MainActivity extends Activity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.mytextview); // Call Go function. String greetings = Hello.greetings("http://baidu.com"); mTextView.setText(greetings); }}Copy the code

Add network access, program run up, will be able to see the network return information display.

conclusion

Go language has shine in the server side, Qiniu, Toutiao, Ali have proved the inherent advantages of GO, simple code, unified format, concise grammar, multi-core high concurrency, improve the development efficiency at the same time, both comparable to C, C++ running speed, is bound to become the next generation of classic language!

At the present stage, GO only supports OpenGl drawing interface in the mobile field, which is still unable to shake the status of Java and Object-C. However, with the support of Google, I believe that Java will be replaced in the android field in the near future, and the response speed of Android, which is criticized by people, will be improved qualitative!