Go: x/mobile: update documentation for module mode

Created on 20 Mar 2020  ยท  12Comments  ยท  Source: golang/go

What version of Go are you using (go version)?

go version go1.13.6 windows/amd64

$ go version

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Rohan\AppData\Local\go-build
set GOENV=C:\Users\Rohan\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Projects\GoProjects;
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Rohan\AppData\Local\Temp\go-build891854990=/tmp/go-build -gno-record-gcc-switches```
<details><summary><code>go env</code> Output</summary><br><pre>
$ go env

</pre></details>

### What did you do?
I'm trying to generate android library using gomobile, I have this code given below, I want to generate an android library from it but Gomobile gives me error as it can't find any exported names but I think ```GetKeys``` should be considered  as an exported name

package whisper

import (
"context"
"fmt"
"log"

"github.com/ethereum/go-ethereum/whisper/shhclient"

)

func GetKeys() {
client, err := shhclient.Dial("ws://10.0.2.2:8546")
if err != nil {
log.Fatal(err)
}
keyID, err := client.NewKeyPair(context.Background())
if err != nil {
log.Fatal(err)
}

fmt.Println(keyID) // 0ec5cfe4e215239756054992dbc2e10f011db1cdfc88b9ba6301e2f9ea1b58d2
publicKey, err := client.PublicKey(context.Background(), keyID)

if err != nil {
    log.Fatal(err)
}

fmt.Println(publicKey)

}

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->



### What did you expect to see?
Android library generated

### What did you see instead?

gomobile: C:UsersRohangobingobind.exe -lang=go,java -outdir=C:UsersRohanAppDataLocalTempgomobile-work-181241247 github.com/rohankeskar19/whisper failed: exit status 1
no exported names in the package "github.com/rohankeskar19/whisper"
no exported names in the package "github.com/rohankeskar19/whisper"
no exported names in the package "github.com/rohankeskar19/whisper"
no exported names in the package "github.com/rohankeskar19/whisper"
unable to import bind/java: go [-e -json -compiled=true -test=false -export=false -deps=false -find=true -- golang.org/x/mobile/bind/java]: exit status 2: go: finding golang.org/x/mobile latest

runtime/cgo

gcc_android.c:6:25: fatal error: android/log.h: No such file or directory
#include
^
compilation terminated.
```

Documentation NeedsInvestigation help wanted mobile modules

Most helpful comment

Seems like we need to update the gomobile documentation (http://golang.org/wiki/Mobile?) to clarify how to use it in module mode.

All 12 comments

gcc_android.c:6:25: fatal error: android/log.h: No such file or directory I'd say your android sdk isn't installed properly or in the wrong path.

It works for another project with GO111MODULE=off

27234 or #37902 might be related.

@hyangah @bcmills (for potential modules issue)

https://github.com/rohankeskar19/whisper shows an empty repo, so that error message seems correct.

set GOMOD=NUL in the go env output indicates that you are working outside of a module, so commands will resolve the latest github.com/rohankeskar19/whisper from upstream (which is an empty repo). Most likely this would not compile at all using go 1.14 or newer.

See https://blog.golang.org/using-go-modules and https://golang.org/doc/code.html for tutorials on setting up a new Go project using modules. I'm not sure if there is any gobind-specific documentation on working with modules.

CC @hajimehoshi @hyangah @steeve

https://github.com/rohankeskar19/whisper shows an empty repo, so that error message seems correct.

set GOMOD=NUL in the go env output indicates that you are working outside of a module, so commands will resolve the latest github.com/rohankeskar19/whisper from upstream (which is an empty repo). Most likely this would not compile at all using go 1.14 or newer.

See https://blog.golang.org/using-go-modules and https://golang.org/doc/code.html for tutorials on setting up a new Go project using modules. I'm not sure if there is any gobind-specific documentation on working with modules.

CC @hajimehoshi @hyangah @steeve

I tried to generate bindings from my local environment, and this error occured before creating the repo, I was able to generate bindings after locally downloading all the packages required in the go-ethereum package and setting GO111MODULE=off

Your go env output indicates set GO111MODULE=on, meaning that you at some point explicitly enabled module mode.

In module mode, your dependencies are determined by the main module's go.mod file, not what is in your local GOPATH.

Your go env output indicates set GO111MODULE=on, meaning that you at some point explicitly enabled module mode.

In module mode, your dependencies are determined by the main module's go.mod file, not what is in your local GOPATH.

Yeah, I know I disabled it later, And even for an package which contains a simple program as

package test

import (
    "fmt"

)

func SayHello() {
    fmt.Println("Inside Hello")

}

I get the same error as above when GO111MODULE is set on, However when it is off It seems to work fine

When working with a local module, run gomobile bind in the main module where it can find the local module, so the go build system can find the local module.

I successfully ran gomobile bind in modules mode

$ tree
.
โ”œโ”€โ”€ foo.go
โ”œโ”€โ”€ go.mod
โ””โ”€โ”€ go.sum

$ cat foo.go
package foo

import (
    "fmt"

)

func SayHello() {
    fmt.Println("Inside Hello")

}

$ gomobile bind -target=ios 
$ grep SayHello ./Foo.framework/*
Binary file ./Foo.framework/Foo matches
...

When working with a local module, run gomobile bind in the main module where it can find the local module, so the go build system can find the local module.

I successfully ran gomobile bind in modules mode

$ tree
.
โ”œโ”€โ”€ foo.go
โ”œโ”€โ”€ go.mod
โ””โ”€โ”€ go.sum

$ cat foo.go
package foo

import (
  "fmt"

)

func SayHello() {
  fmt.Println("Inside Hello")

}

$ gomobile bind -target=ios 
$ grep SayHello ./Foo.framework/*
Binary file ./Foo.framework/Foo matches
...

Ok, It works now! Thanks.

Seems like we need to update the gomobile documentation (http://golang.org/wiki/Mobile?) to clarify how to use it in module mode.

Seems like we need to update the gomobile documentation (http://golang.org/wiki/Mobile?) to clarify how to use it in module mode.

# make `gomobile` works fine with `go module` than the old `$GOPATH` mode.

# 0) 
install   go 1.14

# 1) unset env var for shell
unset GO111MODULE
unset GOMOD 

# 2) set `go module`  to `auto` for the best compatibility
go env -w GO111MODULE=auto

# 3) restart your terminal for safe

# 4) create `go module` configuration file: `go.mod` and `go.sum`
go mod init demo_android_go_mobile

# 5) build your demo go library
gomobile bind -v  -o app/lib/mobile.aar -target=android ./libmobile/src/go/mobile

# 5.1) my project structure

โ•ฐโ”€> tree .
.
โ”œโ”€โ”€ app
โ”‚ย ย  โ”œโ”€โ”€ lib
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ mobile-sources.jar
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ mobile.aar
โ”‚ย ย  โ””โ”€โ”€ src
โ”‚ย ย      โ””โ”€โ”€ java
โ”œโ”€โ”€ go.mod
โ”œโ”€โ”€ go.sum
โ””โ”€โ”€ libmobile
    โ””โ”€โ”€ src
        โ””โ”€โ”€ go
            โ””โ”€โ”€ mobile
                โ””โ”€โ”€ mobile.go

# 5.2) file `mobile.go`

package mobile

import "fmt"

func SayHello() {
      fmt.Println("Hello Mobile")
}

func SayHelloWithParams(name string) {
      fmt.Println("Hello", name)
}

func SayHelloWithParamsAndReturn(name string) string {
      return "Hello" + name
}

func SayHelloWithParamsAndReturnAndException(name string) (string, 
error) {
      return "Hello" + name, fmt.Errorf("some error")
}

Was this page helpful?
0 / 5 - 0 ratings