How to config a simple Go project - go

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.

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...

visual studio code golang package import problem

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/

Implicit dependencies to find go stdlib packages

I have a stripped down environment, where I want to use go at a custom path.
printenv gives me:
GOOS=linux
GOROOT=/mygo
GOHOSTOS=linux
GOARCH=amd64
TMPDIR=/mytmp
GOHOSTARCH=amd64
GOPATH=/mysrcs
PWD=/home/andreas
Now if I try to compile go code, it fails to find the stdlib:
could not import fmt (cannot find package "fmt" in any of:
/mygo/src/fmt (from $GOROOT)
If I do find /mygo | grep fmt, I get:
/mygo/pkg/linux_amd64/fmt.a
When I use the system go (normal bash environment), it works fine. What implicit dependencies does go need to find the stdlib packages?
Thanks to #JimB, I managed to get it working.
I indeed want binary-only packaging of stdlib.
Discussion online is that $GOROOT/pkg is a valid use-case for binary distribution, while $GOPATH/pkg might get deprecated soonish, so beware.
If you want to provide binary-only packages, https://github.com/tcnksm/go-binary-only-package shows how to create (annotated) src directory structure, in order for go to stop searching for source files.

How does Go import resolving dependencies?

In Go, when I import a dependency like:
import "github.com/spf13/viper"
Which directories will Go look into, and in which order?
Lookup order:
The vendor folder.
The standard packages.
GOPATH folder.
Run go help gopath to learn more on how Go search each directory.
A quick and easy way to see the search order and exact paths searched n a particular system is to run go build err.
This produces output similar to this:
can't load package: package err: cannot find package "err" in any of:
C:\go\src\err (from $GOROOT)
\\FREENAS\Global Documents\Projects\Go\src\err (from $GOPATH)

Golang import package error

go:5:2: cannot find package "github.com/googollee/go-socket.io" in any of:
/usr/local/go/src/github.com/googollee/go-socket.io (from $GOROOT)
/Users/YoungHoonKim/Documents/chat/src/github.com/googollee/go-socket.io (from $GOPATH)
I'm getting this error while trying to compile my chat.go file with LiteIDE. I'm following a guide to build a chatserver, so I don't know where this package should be. any help?
Double-check your actual go work directory with echo $GOPATH in your terminal.
This will be your starting point to identify if you have this package at all, for example you should be able to see this package doing cd $GOPATH/src/github.com/googollee/go-socket.io.
For more details on correct directory location (workspace in general) please check https://golang.org/doc/code.html

Resources