What does it mean the code in my directory "expects" a particular import? - go

A Go language tutorial uses the "quote" package to get familiar quotes. When I give the command "go get -v rsc.io/quote", I get the message
code in directory.../github/hello/src/rsc.io/quote/v3 expects import "rsc.io/quote"
So I went there and looked at the code. It has an import statement:
import "rsc.io/quote/v3"
So I thought maybe I had the wrong version of quote. Accordingly, I tried "go get -v rsc.io/quite/v3" THis produces the message:
code in directory .../rsc.io/quote/v3 expects import "rsc.io/quote."
This seems circular: asking without a version number produces a requirement for a versioned package, but trying to get a versioned package produces a requirement without a version number.

If you continue the tutorial to the end you will see that the addition of a go module, and the subsequent initialisation, will resolve your issue. The .mod file will pull in the required dependencies.

Can you try
go get -v rsc.io/quote
get "rsc.io/quote": found meta tag get.metaImport{Prefix:"rsc.io/quote", VCS:"git", RepoRoot:"https://github.com/rsc/quote"} at //rsc.io/quote?go-get=1
rsc.io/quote (download)
get "rsc.io/sampler": found meta tag get.metaImport{Prefix:"rsc.io/sampler", VCS:"git", RepoRoot:"https://github.com/rsc/sampler"} at //rsc.io/sampler?go-get=1
rsc.io/sampler (download)
github/gospace/src/rsc.io/quote/quote.go:8:8: code in directory /Users/james/github/gospace/src/rsc.io/quote/v3 expects import "rsc.io/quote"
cat $GOPATH/src/rsc.io/quote/v3/go.mod
module rsc.io/quote/v3
require rsc.io/sampler v1.3.0
~> cat $GOPATH/src/rsc.io/quote/v3/go.sum
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/sampler v1.99.99 h1:7i08f/p5TBU5joCPW3GjWG1ZFCmr28ybGqlXtelhEK8=
rsc.io/sampler v1.99.99/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
More about go get vs go install. What is the difference between go get and go install?
Can you please check your GO env path settings:
export GOPATH=$HOME/github/<your_workspace_name>
export GOBIN=$GOPATH/bin
export GOROOT=/usr/local/go
export PATH=$PATH:/usr/local/bin:/usr/local/go/bin:$GOPATH/bin
Here is a good source for setting up workspace:
https://golang.org/doc/gopath_code.html

As #MartinBennett mentioned, it's already mentioned in the tutorial but may be not so clear.
Once you add import "rsc.io/quote" in your hello.go file, you will need to run these commands in your terminal:
1-
go mod init hello
2-
go run hello.go

Related

Trouble with go get install for mcumgr

I am trying to install MCUMgr on MACos. Here is a link for mcumgr: https://docs.zephyrproject.org/latest/services/device_mgmt/mcumgr.html
I install the Go Programming Language and enter this command:
go install github.com/apache/mynewt-mcumgr-cli/mcumgr#latest
Upon doing this I get the following error:
go/pkg/mod/golang.org/x/sys#v0.0.0-20200223170610-d5e6a3e2c0ae/unix/zsyscall_darwin_amd64.go:28:3: //go:linkname must refer to declared function or variable
I then googled and found the following from stackoverflow: Go 1.18 build error on Mac: "unix/syscall_darwin.1_13.go:25:3: //go:linkname must refer to declared function or variable"
This gives me the following error:
go: golang.org/x/sys: unrecognized import path "golang.org/x": parse https://golang.org/x?go-get=1: no go-import meta tags ()
I am stuck right now on how to install MCUMgr for MACos and wondering if someone has previously had the same issues?
I used go 1.17 instead and used:
go get github.com/apache/mynewt-mcumgr-cli/mcumgr
Then into the bin folder where it is installed use sudo ./mcumgr to run.

could not import google.golang.org/grpc (cannot find package "google.golang.org/grpc"

Hi I installed golang grpc in my vscode project with some go get commands,
go get -u google.golang.org/protobuf/cmd/protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go
go get -u google.golang.org/grpc/cmd/protoc-gen-go-grpc
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc
from https://stackoverflow.com/a/62872353/4159198
my project builds/runs inside vscode and on the command line but autocomplete gives me an error,
could not import google.golang.org/grpc (cannot find package "google.golang.org/grpc" in any of
/usr/local/go/src/google.golang.org/grpc (from $GOROOT)
/home/john/go/src/google.golang.org/grpc (from $GOPATH))
Find from my home directory:
find * -iname google.golang.org
go/src/google.golang.org
go/pkg/mod/google.golang.org
go/pkg/mod/cache/download/google.golang.org
go/pkg/mod/cache/download/sumdb/sum.golang.org/lookup/google.golang.org
My .profile bottom,
export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:~/go
export GOPATH=/home/john/go
export PATH=$PATH:$GOPATH/bin
I tried copying the source from pkg to src directory.
Try to launch go mod init on every program you want to compile.
If there was packages to imported and it missing, there will be a lightbulb icon that you can click on to automatically go get the packages you want
On your go.mod will there written
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.25.0 // indirect

go get fyne/io/driver/gl#v1.0.1 unrecognized import path

Trying to follow the installation guide here and most of the process is working. I'm having an issue with building it however.
When trying to build I'm getting this message:
C:...\gameboy.live>go build -o gbdotlive main.go
......\go\pkg\mod\fyne.io\fyne#v1.0.1\driver\gl\gl.go:20:2: missing go.sum entry for module providing package github.com/goki/freetype (imported by fyne.io/fyne/driver/gl); to add:
go get fyne.io/fyne/driver/gl#v1.0.1
Running the suggested command however raises another prompt:
C:...\gameboy.live>go get fyne.io/driver/gl#v1.0.1
go get fyne.io/driver/gl#v1.0.1: unrecognized import path "fyne.io/driver/gl": reading https://fyne.io/driver/gl?go-get=1: 404 Not Found
I've tried going for the version 1.4.3 driver too which raises a different error:
C:...\gameboy.live>go get fyne.io/fyne/gl#v1.4.3
go get: module fyne.io/fyne#v1.4.3 found, but does not contain package fyne.io/fyne/gl
Anybody familiar with this issue? FYI I'm on Windows and have MinGw installed already.
Just run go mod tidy before exec go build -o gbdotlive main.go.Have a try.

Error "go.mod has post-v0 module" when I try "go get google.golang.org/grpc/reflection"

I am trying to add reflection to my working Go-based gRPC. I am getting an error when I try to fetch the reflection package:
$ GO111MODULE=on go get google.golang.org/grpc/reflection
go: github.com/google/go-github#v0.0.0-20190119011113-56cb1dd99043: go.mod has post-v0 module path "github.com/google/go-github/v21" at revision 56cb1dd99043
go: error loading module requirements
I can't find any reference to "go-github/v21" anywhere in my source, or in any of the packages in my GOPATH.
I also can't figure out what this error message means.
I am using Go version go1.11.5.
Thanks.

Cannot find package "google/protobuf"

I am compiling an open source project written in go (openblockchain). I get the following error when I do go build. Can anyone help me with this issue
Compilation Error
> go build
../go/src/github.com/openblockchain/obc-peer/openchain/util/utils.go:28:2: cannot find package "google/protobuf" in any of:
/usr/src/pkg/google/protobuf (from $GOROOT)
/home/vichu/go/src/google/protobuf (from $GOPATH)
Additional Information
I referred the question here in Stack Overflow but still no luck in solving the issue. Here is some more information about what all I have:
Protoc version is up to date.
> protoc --version
libprotoc 3.0.0
My environment variables
> echo $GOPATH
/home/vichu/go
> echo $GOBIN
/home/vichu/go/bin
Protobuf has been built using the README.
~/go/src/github.com/golang/protobuf$ ls
AUTHORS CONTRIBUTORS jsonpb LICENSE Makefile Make.protobuf proto protoc-gen-go proto.pb.go ptypes README.md
Update
I did the following Util.go in source code as mentioned in answer.
The source code is open source and here is the link
- gp "google/protobuf"
+ gp "github.com/google/protobuf"
When I do go get, the below is the error
> go get github.com/google/protobuf
can't load package: package github.com/google/protobuf: no buildable Go source files in /home/vichu/go/src/github.com/google/protobuf
Firstly, your import is wrong, you are trying to import a C++ package, not a golang package. It needs to be:
import ("github.com/golang/protobuf/proto")
If you don't have this package installed already, you need to run from command line:
go get github.com/golang/protobuf/proto
I think, at the end of your *.pb.go file you also do not have some thing like "gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00 ...."
The issue is we use a wrong compiler for generating.
So:
1) I reinstall protobuf from:
https://github.com/google/protobuf/releases
2) Then (I'm using ubuntu and it has a proto compiler also):
apt remove protobuf-compiler
Rebuild the *.proto file. It fixed the bug.

Resources