How should I resolve this import problem with gorilla/mux? - go

could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of
C:\Program Files\Go\src\github.com\gorilla\mux (from $GOROOT)
C:\Users\lenovo\go\src\github.com\gorilla\mux (from $GOPATH))compilerBrokenImport
I have installed gorilla mux using the cmd "go get github.com/gorilla/mux" but I'm getting this error.enter image description here

Looks like you didn't run "go mod init" before the "go get".
In cmd, try this:
cd [the dir of your source code]
go mod init
go get github.com/gorilla/mux
For more information run:
go help mod
Afterwards, you might also need to restart VS Code.

Related

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

Build dependencis for aws-sdk-go fails on Go version 1.13

I have been trying to build a module on Go v1.13 with dependencies on github.com/aws/aws-lambda-go and github.com/aws/aws-sdk-go which fail on the two imports:
"github.com/aws/aws-sdk-go/aws/service/s3"
"github.com/aws/aws-sdk-go/aws/service/s3/s3manager"
The stderr is as follows for GOOS=linux GOARCH=amd64 go build -o dist/api ./api where the api directory contains my module definition:
api/main.go:11:2: cannot find package "github.com/aws/aws-sdk-go/aws/service/s3" in any of:
/usr/local/go/src/github.com/aws/aws-sdk-go/aws/service/s3 (from $GOROOT)
/u/go/src/github.com/aws/aws-sdk-go/aws/service/s3 (from $GOPATH)
api/main.go:12:2: cannot find package "github.com/aws/aws-sdk-go/aws/service/s3/s3manager" in any of:
/usr/local/go/src/github.com/aws/aws-sdk-go/aws/service/s3/s3manager (from $GOROOT)
/u/go/src/github.com/aws/aws-sdk-go/aws/service/s3/s3manager (from $GOPATH)
Honestly, I have no clue why this is happening and any inputs would be appreciated.
Already tried using go get to ensure that the dependencies have been pulled:
$ go get github.com/aws/aws-sdk-go
and the requested import paths are present under $GOPATH/src/github.com/aws/aws-sdk/go/aws/service/s3 and $GOPATH/src/github.com/aws/aws-sdk/go/aws/service/s3/s3manager
Also, tried clearing the cache using go clean --cache --modcache whilst removing previously pulled modules.
On closer inspection, something that I completely overlooked, the import path is /u/go/src/github.com/aws/aws-sdk-go/aws/service/s3 instead of /u/go/src/github.com/aws/aws-sdk-go/service/s3 with the former having an additional aws subpath inside aws-sdk-go.
Just realized the copy/paste error I had made in the code.
import (
"github.com/aws/aws-sdk-go/aws/service/s3"
"github.com/aws/aws-sdk-go/aws/service/s3/s3manager"
)
instead of
import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

Cannot find package under GOPATH

I am trying to install the dependencies of my project with glidebut unfortunately it fails with the following message:
main.go:7:2: cannot find package "github.com/arschles/go-in-5-minutes/episode13/models" in any of:
/Users/theo/go-workspace/src/github.com/thitami/go-in-5-minutes/episode13/vendor/github.com/arschles/go-in-5-minutes/episode13/models (vendor tree)
/usr/local/Cellar/go/1.8.3/libexec/src/github.com/arschles/go-in-5-minutes/episode13/models (from $GOROOT)
/Users/theo/go-workspace/src/github.com/arschles/go-in-5-minutes/episode13/models (from $GOPATH)
Running a go env, this is my env variables of interest:
GOPATH="/Users/theo/go-workspace"
GOROOT="/usr/local/Cellar/go/1.8.3/libexec"
Please be advised that I am zsh and I am exporting the GOPATH inside the .zshrc file like this:
export GOPATH=HOME/go-workspace
Any ideas are appreciated
* UPDATE *
As requested this is the piece of code with the call to the models package:
import (
"database/sql"
"log"
"github.com/arschles/go-in-5-minutes/episode13/models"
_ "github.com/mxk/go-sqlite/sqlite3"
)
You can only set the gopath once and you'll have to reset it every time you change packages. Think of it as a virtualenv. One way around it is to install the packages global or by using something like gvm

"cannot find package" error on godoc playable examples

I'm trying to make palyable example on godoc.
OS X Yosemite 10.10.5
Go: go1.7.4 darwin/amd64
test code:
$GOPATH/src/hoge/hoge_test.go
package hoge_test
import (
"fmt"
"hoge"
"testing"
)
func ExampleHoge() {
fmt.Println(hoge.Hoge())
// Output:
// hoge!!
}
The test passes:
$ go test hoge
ok hoge 0.011s
Godoc
$ godoc -play -http=:8080
I can see the hoge package's example playground as I expected on a web browser, but an error below occurs when I 'Run' the example.
tmp/sandbox389904218/main.go:5:2: cannot find package "hoge" in any of:
/usr/local/go/src/hoge (from $GOROOT)
/go/src/hoge (from $GOPATH)
I set my own GOPATH, but it's not /go as shown in the error.
What I need to run examples.
Additional
I've got a reason of the error above.
Godoc playable example redirects to play.golang.org to run the code, so the GOPATH in the error seems to be on the play.golang.org environment.
I'm still not sure how to run my own pkg example though...
This is because godoc uses the golang.org/x/tools/playground tool, which just proxies requests to golang.org. It does not appear that you can run your example functions unless they are in a package that is published on golang.org. Here's the relevant file in github: https://github.com/golang/tools/blob/master/playground/common.go which is referenced by the godoc tool here: https://github.com/golang/tools/blob/master/cmd/godoc/play.go

Go install: “Can't load package” (even though GOPATH is set)

I'm just getting started with the Go programming language and installed Go using the Windows installer from the website. I tested installation by using go run hello.go and that works. The problem comes when I try to build my first program:
$ echo $GOROOT
C:\Go\
$ echo $GOPATH
/cygdrive/c/Users/Paul/Documents/Home/go
mkdir -p $GOPATH/src/hello
Inside that directory I have a simple hello.go program:
package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
The problem comes when I try to build and install:
$ go install hello
can't load package: package hello: cannot find package "hello" in any of:
C:\Go\src\hello (from $GOROOT)
\cygdrive\c\Users\Paul\Documents\Home\go\src\hello (from $GOPATH)
GOPATH environment variable must contain valid path.
\cygdrive\c\Users\Paul\Documents\Home\go\src\hello is not a valid path on Windows.
Try setting GOPATH=c:\Users\Paul\Documents\Home\go instead.

Resources