visual studio code golang package import problem - go

could someone enlighten me on how to get simple import statements in vsc to work for an example main.go file I borrowed from somewhere? The following observations apply:
go version go1.17.2 windows/amd64;
I can get this program to run (using modules) without any problems;
The imports in my main program are for fyne like e.g. "fyne.io/fyne/v2/app" etc.;
$GOROOT points to something like C:\Program files\Go.. ;
$GOPATH points to C:\Users\xyz\go.... ;
My current source code is located under $GOPATH\src... but can be moved somewhere else if
needed;
In my source subdirectory there is a main.go and I added a module file as follows:
go mod init main & go mod tidy
this results in the following output : go: finding module for package fyne.io/fyne/v2/app go: finding module for package fyne.io/fyne/v2 go: finding module for package fyne.io/fyne/v2/container go: finding module for package fyne.io/fyne/v2/canvas go: found fyne.io/fyne/v2 in fyne.io/fyne/v2 v2.1.2 go: found fyne.io/fyne/v2/app in fyne.io/fyne/v2 v2.1.2 go: found fyne.io/fyne/v2/canvas in fyne.io/fyne/v2 v2.1.2 go: found fyne.io/fyne/v2/container in fyne.io/fyne/v2 v2.1.2
So it seems that everything thats needed is there and I can subsequently run the program.
However when displaying my main.go source in vsc (1.63.0) it complains about not being able to find(import) any of the required fyne packages/imports. It lists that it looks in the \src subdirectories of both $GOROOT and $GOPATH but can't find anything. This is actually correct because there is nothing there. The closest I can find are fyne packages located under
C:\Users\xyz\go\pkg\mod\fyne.io\fyne...
Looked at all kinds of documentation but have no clue of whats exactly the problem and how to solve this.
Thanks in advance
Peter

Your dependencies should be located in the right place, i.e. where the build tool, in this case vsc is expecting them. So move them there, and set your GOPATH correctly as explained here: https://www.c-sharpcorner.com/article/how-to-setup-golang-with-vscode/

Related

Cannot find package when running golang test

When I run go tests I get the error:
cannot find package "github.com/stretchr/testify/assert" in any of:
/usr/local/Cellar/go/1.17.6/libexec/src/github.com/stretchr/testify/assert (from $GOROOT)
/Users/MyName/go/src/github.com/stretchr/testify/assert (from $GOPATH)
I do however see that my local installation of stretchr and other tools ARE in /Users/MyName/golang/pkg/mod/github.com
How do I get golang to look in the right place for these installations? I thought it would be grabbing the resource from github if the import is pointing to the repo.
Are my installations in the "wrong" spot, and should I move them to where golang would look for them by default?
I have been able to test using stretchr before, but upgrading the package didn't change anything.
Running go get github.com/stretchr/testify and go mod tidy also didn't seem to change the behavior.
Thanks in advance for your help.
Run "go mod init" whilst in your folder with your code, then go mod tidy.
It should work, if not, try go get ... then try this again.
Some IDE's such as VSCode delete imports when saving if they're unrecognised, or not used, so make sure when you run go mod init, and tidy, that the import is actually in the file
I closed VS Code and re-opened, and it's working now...

Trouble with using gorilla/mux package in mac

I am trying to learn how to build a webserver using go and mux. I am importing mux to the main.go file as import github.com/gorilla/mux. However, when I am trying to run the code. I get the following error
no required module provides package github.com/gorilla/mux: go.mod file not found in current directory or any parent directory; see 'go help modules'
My GOPATH is /Users/michiokaku/Study/go
The overall structure of my directories is
go___
pkg
bin
my_codes___
main.go
Inside pkg, I found a directory named mux#v1.8.0 in the path pkg/mod/github.com/gorilla. I think this is what I downloaded using go get -u github.com/gorilla/mux. But when the code is running, I am getting errors.
What is the issue here? How do I solve this?
PS: I am using mac.
Read through Tutorial: Getting Started with Go, if you haven't seen it already. It matches your situation pretty closely.
In short:
Run go mod init example.com/projectname, replacing the last argument with the name for your module. This will create a go.mod file in the current directory that will track your dependencies. Your module's name will be a prefix for all packages within your module.
Run go mod tidy or go get github.com/gorilla/mux to add github.com/gorilla/mux as a dependency.
You mentioned you saw a directory pkg/mod/github.com/gorilla earlier. This is part of Go's module cache, shared by all projects.

How to config a simple Go project

I'm trying to follow the Writing an intepreter in Go book, by Thorsten Ball, and in the first chapter he establish this simple scheme
file /Users/myuser/projects/monkey/token/token.go
file /Users/myuser/projects/monkey/lexer/lexer.go
file /Users/myuser/projects/monkey/lexer/lexer_test.go
In lexer/lexer.go and lexer/lexer_test.g the files start as
package lexer
And in lexer_test.go the imports are
import (
"testing"
"monkey/token"
)
Then he says that for running the test, I have to run (from /Users/myuser/projects/monkey directory):
go test lexer/lexer_test.go
But when I do this, I receive the error:
lexer/lexer_test.go:6:2: cannot find package "monkey/token" in any of:
/usr/local/opt/go/libexec/src/monkey/token (from $GOROOT)
/Users/myuser/golang/src/monkey/token (from $GOPATH)
FAIL command-line-arguments [setup failed]
FAIL
I've been trying to understand how to configure the packages in go, but I found a lot of very complicated documentation about GOPATH, GOROOT and go.mod. I've been trying all this approach without get rid of the issue.
Can someone help me please? I'm sure is a simple fix but I cannot figure it out :(
As the error message says, the compiler couldn't find the package locally.
Are you sure you have installed the package?
You may need to do go get [packagename]
For e.g., go get golang.org/x/tools/cmd/goimports
For Golang, It will find package first from go root then go path then import from outside. So basically, you should have that package monkey/token in your go root or go path.
I don't think it is about goimport because monkey/token didn't seem to be official lib that can be import.
When I first try Golang I have the same problem. I solve by create that package in go path (your working dir) or create that package from your book.

go module #latest found but does not contain package

I'm trying to make use of go module for the first time. What exactly the following error message is telling me?
module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
It happens during go build, whereas go get is just fine:
$ go get -v github.com/mkideal/cli
go: github.com/mkideal/cli upgrade => v0.2.2
but not go get -v ./..., which gave me the same error as above. My proxy setting looks OK:
$ go env | grep GOPROXY
GOPROXY="https://proxy.golang.org,direct"
Is it a problem of the go module/package I'm trying to use, or my own code's problem? -- I took a look at
https://github.com/mkideal/cli/blob/master/go.mod and it seems fine to me.
See the following update for details.
How can I overcome the situation? (I'm getting the same error message for my own repo as well)
UPDATE:
Here is the full log how I'm getting the above error:
prepare /tmp/015-file from https://github.com/mkideal/cli/blob/master/_examples/015-file
do go mod init
then go build
Now the details:
$ cd /tmp/015-file
$ GO111MODULE=on
$ go mod init github.com/mkideal/cli/015-file
go: creating new go.mod: module github.com/mkideal/cli/015-file
$ cat go.mod
module github.com/mkideal/cli/015-file
go 1.14
$ go build
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
main.go:6:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
main.go:7:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
$ go get -v github.com/mkideal/cli
go: github.com/mkideal/cli upgrade => v0.2.2
$ go get -v ./...
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
go: finding module for package github.com/mkideal/cli
go: finding module for package github.com/mkideal/cli/ext
main.go:6:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli
main.go:7:2: module github.com/mkideal/cli#latest found (v0.2.2), but does not contain package github.com/mkideal/cli/ext
$ go version
go version go1.14.1 linux/amd64
Try clearing cache:
go clean -modcache
For more info on how this command works, use go help clean
In my case cleaning cache didn't help.
Running go install in a project root printed no Go files in ... and that was the root cause, in the same time running go install gitlab.com/.... printed info about a missing package.
What had to be done was creating a go file in a project root directory with main function.
Update to go version go1.14.3 linux/amd64
Clear go module cache
don't know which one solved the problem (or both), now AOK.
I had the same error, but in my case I was attempting to import a module that made available only resource files, and no go pkgs. Adding an empty go file in the module with a package declaration solved it.
In my case, go.mod files were under src, after moving the go.mod file into one level up, then it works
Refer the Samples below,
directory structure when "package not found" error
dir1/src/
main.go
go.mod
go.sum
directory structure after fix
dir1/
go.mod
go.sum
src/
main.go
I had a similar problem. In my case the package name was not matching the name of the folder it resided in.

Using modules, newly installed package cannot be referenced within project

go version go1.11.4 darwin/amd64
GOPATH has been unset but was previously set to $HOME/Development/go
Project path is under $HOME/code/
I’m able to successfully (no errors at least) install the go-sql-driver/mysql package using the command
go get github.com/go-sql-driver/mysql#v1
When I include a reference to that package in an import statement
import(
_ "github.com/go-sql-driver/mysql")
in my code I see the error
could not import github.com/go-sql-driver/mysql (can’t find import:
“github.com/go-sql-driver/mysql”)
I have run go mod init in my project root and it creates a go.mod file. When I run the go get command I see a require statement added to that file for the package. But it seems the files for the package get installed in the default $HOME/go directory (since I've unset GOPATH).
Should I be doing things differently so that my import statement can find the newly installed package? Using modules shouldn't all the packages be installed in the project path somewhere?
Should I be doing things differently so that my import statement can find the newly installed package?
No. With modules there is no need to install the packages upfront at all.
Using modules shouldn't all the packages be installed in the project path somewhere?
No. They get downloaded somewhere in some format and used from that location but they are not "installed" like in the old GOPATH variant of go get.
Show output of go env and what go mod vendor produces.
I'm pretty sure I was doing things wrong. I was able to resolve this after referencing the following closely the steps documented at golang modules wiki. The summary is that there is no need to "install" a package via 'go get'. Instead simply make sure your project is initialized to use modules using the 'go mod init' command and then include the package name in an import statement. The next build event will pull down the package and all its dependencies.

Resources