3

I am a go newbie. I am trying to install gomobile for evaluation on MacOS BigSur but can't get past the init step.

running go version returns go version go1.17.6 darwin/amd64

Following the directions, the gomobile install appears to work properly (after explicitly adding gomobile to my PATH), but running init or version fails.

gomobile: go install golang.org/x/mobile/cmd/gobind failed: exit status 1
go install: version is required when current directory is not in a module
    Try 'go install golang.org/x/mobile/cmd/gobind@latest' to install the latest version
gomobile version unknown: cannot test gomobile binary: exit status 1, no required module provides package golang.org/x/mobile/cmd/gomobile: go.mod file not found in current directory or any parent directory; see 'go help modules'

I'm guessing this has something to do with environment variables but any suggestions and/or help would be appreciated.

Mike M
  • 4,358
  • 1
  • 28
  • 48
  • It looks like the error is saying you need to create a modulue by running: ‘go mod init ’ and then run the install for gomobile from that directory – NO_GUI Jan 10 '22 at 21:21
  • @NO_GUI - thanks, you were correct. I was able to successfully install `gomobile` and run `gomobile init` if I added it as a dependency to another module. – Mike M Jan 11 '22 at 14:32
  • Cool! Glad you got it working – NO_GUI Jan 13 '22 at 03:09

1 Answers1

5

I did manage to finally get gomobile set up, but the process was painful and the official "experimental" documentation was incomplete and not at all helpful. Here's the steps that worked for me on MacOS (BigSur):

Follow steps to install GO from website

Install gomobile

go install golang.org/x/mobile/cmd/gomobile@latest

Install Xcode command line tools. If installed but not found, you may need to run the following:

xcode-select -r

Install Android NDK. This can be done via Android Studio’s Tools->SDK Manager. Need to ensure that the installed version is supported by gomobile since the latest version of the NDK is NOT supported by gomobile.

Update shell to include the following exports:

export PATH=$PATH:/Users/mikem/go/bin
export ANDROID_HOME=/Users/mikem/Library/Android/sdk
export ANDROID_NDK_HOME=/Users/mikem/Library/Android/sdk/ndk/23.1.7779620

Create a working directory

mkdir myworkdir

Create a module directory

mkdir mymodule
cd mymodule

Create a .go file in that module directory and give it some content. Note that the package name should be different than the module name or the bind may complain that it can't find anything

package mymodulelib

import "fmt"

func SayHello() {
        fmt.Println("Hello from mymodule")
}

From the work directory, initialize the module

go mod init mymodule

Install gobind - it will complain that you should be using the go install method instead of the go get (deprecated) but the go install does not work (at least for me)

go get golang.org/x/mobile/cmd/gobind

Go back into the module directory, initialize and then generate iOS and android code

cd mymodule
gomobile init
gomobile bind -target ios
gomobile bind -target android
Mike M
  • 4,358
  • 1
  • 28
  • 48