In certain cases, we need to implement some functionality for mobile, and writing code for every mobile is not only a lot of work, but also difficult to maintain.

One solution we used was to write the Go code and compile it separately for each platform. The Go language also provides cross-platform compilation, but the command is complex and requires many parameters to be configured, making it error-prone. There are several mature libraries that can be used directly, in this case the Gomobile framework.

This article uses Go1.14, Go language installation will not be described.

Installed gomobile

Gomobile can compile go code into mobile applications or ** mobile SDKS. ** This article demonstrates how to compile go code into the SDK.

Installed gomobile:

go get golang.org/x/mobile/cmd/gomobile
Copy the code

If you can’t install it, you can set the domestic Go agent:

go env -w GOPROXY=https://goproxy.cn,direct
Copy the code

Go Mobile supports Go Modules, so you don’t need to create projects in GOPATH. Create a hello project based on the Go Modules, where there is only one file hello. Go, as follows:

package hello import "fmt" func Hello(name string) string { return fmt.Sprintf("Hello %s!" , name) }Copy the code

The Android platform

Install Android Studio and download the latest version.

Download NDK and CMake through Android Studio’s SDK Manager and select Show Package Details in the lower right corner to download the specific version.

Gomobile is not compatible with NDK. I have tested NDK 22 or above and failed. The NDK version used in this article is 21.4.7075529, CMake can download the latest version.

Then configure the environment variables:

Export ANDROID_HOME = ~ / Library/Android SDK export ANDROID_NDK_HOME = ~ / Library/Android/SDK/the NDK / 21.4.7075529Copy the code

Then execute the following command in the directory one level above the Hello project:

# hello is the go project directory $gomobile bind-target = Android helloCopy the code

Compiling the Go code into the SDK uses the bind command. After the above named execution is complete, two files will be produced in production:

hello.aar
hello-sources.jar
Copy the code

Aar = hello.aar = hello.aar = hello.aar = hello.aar = hello.aar = hello.aar = hello.aar = hello.aar = hello

import hello.Hello; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { // Fab.setonclicklistener (new view.onClickListener () {@override public void onClick(View View) {// Call hello here Snackbar.make(view, Hello.hello("ray"), Snackbar.LENGTH_LONG) .setAction("Action", null).show(); }}); }}Copy the code

The IOS platform

To compile to IOS, XCode and the accompanying Command Line Tools need to be installed. The whole process is much easier than that on Android.

Also in the parent directory of the project, execute the following command:

# hello is the go project directory $gomobile bind-target =ios helloCopy the code

If this error occurs:

gomobile: -target=ios requires XCode
Copy the code

Configure Command Line Tools in Xcode:

After the preceding command is executed successfully, a hello. FrameWork directory is generated locally, which is the dependent library on IOS platform. You can simply copy the entire directory to the IOS directory and add it to your project’s dependencies to use it in your code:

Struct ContentView: View {var body: some View {// Call hello (HelloHello("ray")).padding()}Copy the code

summary

At this point, the Go code is running on both Android and IOS, and the tool is essentially a wrapper around the Go command. I’ll write a follow-up article detailing gomobile’s implementation.

The text/Rayjun

This article was first published on wechat official account [Rayjun]