cannot find package "github.com/user../../" in golang while building - go

I am trying to build a project from github source code. I have found some source code importing a package something like below:
import (
"os"
"github.com/bivas/rivi/commands"
"github.com/mitchellh/cli"
)
However, while building a project every time it throws an error:
user-MacBook-Pro:rivi user$ go build rivi.go
rivi.go:6:2: cannot find package "github.com/bivas/rivi/commands" in any of:
/usr/local/Cellar/go/1.7.5/libexec/src/github.com/bivas/rivi/commands (from $GOROOT)
($GOPATH not set)
rivi.go:8:2: cannot find package "github.com/mitchellh/cli" in any of:
/usr/local/Cellar/go/1.7.5/libexec/src/github.com/mitchellh/cli (from $GOROOT)
($GOPATH not set)
how to build this project. Currently I am trying to build this one project into my system.
EDITED:
After running this command go install or go get:
package github.com/bivas/rivi/commands: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/connectors/github: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/autoassign: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/automerge: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/commenter: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/labeler: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/locker: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/sizing: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/slack: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/status: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/bivas/rivi/engine/actions/trigger: cannot download, $GOPATH not set. For more details see: go help gopath
package github.com/mitchellh/cli: cannot download, $GOPATH not set. For more details see: go help gopath

This question is a few years old now. I was experiencing the same problem. The solution for me was that I needed to run go mod init [name_of_main_go_file], which creates the go.mod file. Then you can run go run .
https://golang.org/doc/tutorial/getting-started#call

This problem common if you haven't setup any go path yet. It is necessary.
I am assuming from the command line snap of yours, that you are a UNIX user. Googles official doc recommend that you setup your go path manually . $GOROOT is optional but $GOPATH has to be set if you are trying to get third party libraries.
Edit your ~/.bash_profile to add the following line:
export GOPATH=$HOME/work
Then logout and login or use source ~/.bash_profile
You can read more from here. Assuming your Golang package directory is in work folder inside your home directory. The directory structure of work folder has to look like this,
work
src
pkg
bin

Related

how do you view .go files from imported packages you've installed?

Right now, I have imported a package. In my case it is
import "crypto/ecdsa"
This package contains a file called ecdsa.go. I'd like to view it, however I don't know where to see it. I can't seem to find it in my working directory. Is the file hidden? Or is it installed globally by default?
This package is a part of the Go project. You can find it in your <go-root-path>/src directory.
You can find your "go root path" by running go env GOROOT.
Probable location of Go in linux is /usr/local/go
Go standard library packages are located in GOROOT/src.
For GOROOT, run
go env GOROOT
For example, for package import "crypto/ecdsa" file ecdsa.go on Linux,
$ go env GOROOT
/home/peter/go
$ ls /home/peter/go/src/crypto/ecdsa/ecdsa.go
/home/peter/go/src/crypto/ecdsa/ecdsa.go
$

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.

GOPATH not set in VSCode [duplicate]

I'm trying to install doozer like this:
$ goinstall github.com/ha/doozer
I get these errors.
goinstall: os: go/build: package could not be found locally
goinstall: fmt: go/build: package could not be found locally
goinstall: io: go/build: package could not be found locally
goinstall: reflect: go/build: package could not be found locally
goinstall: math: go/build: package could not be found locally
goinstall: rand: go/build: package could not be found locally
goinstall: url: go/build: package could not be found locally
goinstall: net: go/build: package could not be found locally
goinstall: sync: go/build: package could not be found locally
goinstall: runtime: go/build: package could not be found locally
goinstall: strings: go/build: package could not be found locally
goinstall: sort: go/build: package could not be found locally
goinstall: strconv: go/build: package could not be found locally
goinstall: bytes: go/build: package could not be found locally
goinstall: log: go/build: package could not be found locally
goinstall: encoding/binary: go/build: package could not be found locally
GOPATH is discussed in the cmd/go documentation:
The GOPATH environment variable lists places to look for Go code. On
Unix, the value is a colon-separated string. On Windows, the value is
a semicolon-separated string. On Plan 9, the value is a list.
GOPATH must be set to get, build and install packages outside the
standard Go tree.
GOROOT is discussed in the installation instructions:
The Go binary distributions assume they will be installed in
/usr/local/go (or c:\Go under Windows), but it is possible to install
the Go tools to a different location. In this case you must set the
GOROOT environment variable to point to the directory in which it was
installed.
For example, if you installed Go to your home directory you should add
the following commands to $HOME/.profile:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
Note: GOROOT must be set only when installing to a custom location.
(updated version of Chris Bunch's answer.)
Here is a my simple setup:
directory for go related things: ~/programming/go
directory for go compiler/tools: ~/programming/go/go-1.4
directory for go software : ~/programming/go/packages
GOROOT, GOPATH, PATH are set as following:
export GOROOT=/home/user/programming/go/go-1.4
export GOPATH=/home/user/programming/go/packages
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
So, in short:
GOROOT is for compiler/tools that comes from go installation.
GOPATH is for your own go projects / 3rd party libraries (downloaded with "go get").
First run go env.
If you see that the go isn't installed, you can install it via homebrew or via package and/or other ways.
If you are seeing output then your Go is installed.
It shows you all the envs that are set and are not.
If you see empty for GOROOT:
Run which go (On my computer : /usr/local/go/bin/go)
then export like this export GOROOT=/usr/local/go
If you see empty for GOPATH:
Create any directory anywhere on your computer for go projects in my case: ~/GO_PROJECTS
Then export GOPATH=~/GO_PROJECTS
GOPATH is discussed here:
The GOPATH Environment Variable
GOPATH may be set to a colon-separated list of paths inside which Go
code, package objects, and executables may be found.
Set a GOPATH to use goinstall to build and install your own code and
external libraries outside of the Go tree (and to avoid writing
Makefiles).
And GOROOT is discussed here:
$GOROOT The root of the Go tree, often $HOME/go. This defaults to the
parent of the directory where all.bash is run. If you choose not to
set $GOROOT, you must run gomake instead of make or gmake when
developing Go programs using the conventional makefiles.
I read the go help gopath docs and was still incredibly confused, but found this little nugget from another go doc page:
The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code.
http://golang.org/doc/code.html#GOPATH
Starting with go 1.8 (Q2 2017), GOPATH will be set for you by default to $HOME/go
See issue 17262 and Rob Pike's comment:
$HOME/go it will be.
There is no single best answer but this is short and sweet, and it can only be a problem to choose that name if $HOME/go already exists, which will only happen for experts who already have go installed and will understand GOPATH.
The GOPATH should not point to the Go installation, but rather to your workspace (see https://golang.org/doc/code.html#GOPATH). Whenever you install some package with go get or go install, it will land within the GOPATH. That is why it warns you, that you most definitely do not want random packages from the internet to be dumped into your official installation.
You generally should not set GOROOT explicitly. The go command identifies the appropriate GOROOT automatically based on its own directory location.
GOPATH defaults to $HOME/go. You only need to set it explicitly if you want to put it somewhere else.
GOPATH contains:
Binaries installed using go install, located at $GOPATH/bin.¹
This location can be overridden using the GOBIN environment variable.
A cache of downloaded module source code and checksums, located at $GOPATH/pkg/mod.
This location can be overridden using the GOMODCACHE environment variable.
If you set both GOBIN and GOMODCACHE, and do not set GO111MODULE=off, then GOPATH itself should have essentially no effect.
In addition, in the legacy GOPATH mode (when GO111MODULE=off is also set), GOPATH contains:
Source code used to build packages, stored in a directory tree rooted at $GOPATH/src.
Non-binaries installed using go install, located at $GOPATH/pkg.
Installing non-binary packages is no longer particularly useful: the go command has a cache of built artifacts, which has been required since Go 1.12 even in GOPATH mode.
The build cache is not located within GOPATH. Its location can be set with the GOCACHE environment variable.
¹ Binaries can also be installed using go get on Go 1.17 and earlier, but go install is preferred as of Go 1.16; see https://golang.org/doc/go1.16.
In modern Go, you don't need to set GOPATH or GOROOT. In fact, unless you're doing something very specialized, it's best to have them unset on your system.
Use Go modules. Having installed Go, pick a directory where you want to work. Then:
$ mkdir example
$ cd example
$ go mod init example.com
Note that the module name example.com is arbitrary; if you keep your work on GitHub, this could be something like github.com/your-username/project-name.
The last command will have created a go.mod file; now you can grab dependencies with go get:
$ go get rsc.io/quote
Now your code using this dependency:
$ touch main.go
Place this in main.go:
package main
import (
"fmt"
"rsc.io/quote"
)
func main() {
fmt.Println(quote.Go())
}
And run with:
$ go run .
W.r.t. original question, you can now get your doozer dependency with:
$ go get github.com/ha/doozer
Now you can use the doozer module in your code. And so on. You can also examine the go.mod file in your directory to see the dependencies listed, along with their versions. Each module is self-contained, with its own versions of dependencies. You can have two modules alongside each other, each with its own go.mod file pointing to different versions of some dependency - this will all work OK because of the isolation between modules.
For additional information, start with the official tutorial here. In several chapters, it walks you through the steps shown above, as well as writing your own reusable modules and packages, and importing them from other modules. Additional interactive tutorials are available at https://play-with-go.dev/
GOPATH and GOROOT configurations are deprecated.
You can use the GO module instead.
For example:
mkdir go_app
cd go_app
go mod init go_app
Regarding GOROOT specifically, Go 1.9 will set it automatically to its installation path.
Even if you have multiple Go installed, calling the 1.9.x one will set GOROOT to /path/to/go/1.9 (before, if not set, it assumed a default path like /usr/local/go or c:\Go).
See CL Go Review 53370:
The go tool will now use the path from which it was invoked to attempt to locate the root of the Go install tree.
This means that if the entire Go installation is moved to a new location, the go tool should continue to work as usual.
This may be overriden by setting GOROOT in the environment, which should only be done in unusual circumstances.
Note that this does not affect the result of the runtime.GOROOT() function, which will continue to report the original installation location; this may be fixed in later releases.
As mentioned above:
The GOPATH environment variable specifies the location of your
workspace.
For Windows, this worked for me (in Ms-dos window):
set GOPATH=D:\my_folder_for_go_code\
This creates a GOPATH variable that Ms-dos recognizes when used as follows:
cd %GOPATH%
Lots of answers but no substance, like robots doing cut and paste on what's on their system. There is no need to set GOROOT as an environment variable. However, there is a beneficial need to set the GOPATH environment variable, and if not set it defaults to ${HOME}/go/ folder.
It is the PATH environment variable that you must pay attention because this variable is the variable that can change your go version. Not GOROOT! Forget GOROOT.
Now, if you switch or change to a new go version, your downloaded packages will use the default $HOME/go folder and it will mixed-up with whatever your previous go version was. This is not good.
Therefore, this is where GOPATH you need to define in order to isolate downloaded packages of the new go version.
In summary, forget GOROOT. Think more on GOPATH.
Run go help environment it has documentation for every environment variable that can be listed by go env command
Here is one solution (single user):
GOROOT=$HOME/.local # your go executable is in $GOROOT/bin
GOPATH=$HOME/.gopath
PATH=$GOROOT/bin:$GOPATH/bin:$PATH
go complains if you change .gopath to .go.
I wish they went with how the rust/cargo guys did and just put everything at one place.
There's a command you can use: go env GOPATH
You don't need to explicitly set GOROOT (Modern versions of Go can figure it out on their own based on the location of the go binary that you run).
Also, got the follow error when trying to work with vgo:
go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'
Removing GOROOT, updating my GOPATH and export GO111MODULE="on" resolved the issue.
GOPATH see in here
GOPATH may be set to a colon-separated list of paths inside which Go code, package objects, and executables may be found.
Set a GOPATH to use goinstall to build and install your own code and external libraries outside of the Go tree (and to avoid writing Makefiles).
As of 2020 and Go version 1.13+, in Windows the best way for updating GOPATH is just typing in command prompt:
setx GOPATH C:\mynewgopath
I had to append
export GOROOT=/usr/local/Cellar/go/1.10.1/libexec
to my ~/.bash_profile on Mac OS X
There is also a case where when we use go it compiles all the go files.
So lets say we had one file main.go and later we changed the current file to main_old.go and then added our new main.go file. Then when we build our app all the go files will get compiled. So the error that's happening might be due to compilation error in some other go files.
Once Go lang is installed, GOROOT is the root directory of the installation.
When I exploded Go Lang binary in Windows C:\ directory, my GOROOT should be C:\go.
If Installed with Windows installer, it may be C:\Program Files\go (or C:\Program Files (x86)\go, for 64-bit packages)
GOROOT = C:\go
while my GOPATH is location of Go lang source code or workspace.
If my Go lang source code is located at C:\Users\\GO_Workspace, your GOPATH would be as below:
GOPATH = C:\Users\<xyz>\GO_Workspace
For all newcomers they could do a simply export GOPATH=$HOME/go if you are using Ubuntu or do go help gopath for more information.
in osx, i installed with brew, here is the setting that works for me
GOPATH="$HOME/my_go_work_space" //make sure you have this folder created
GOROOT="/usr/local/Cellar/go/1.10/libexec"
If you are using the distro go, you should point to where the include files are, for example:
$ rpm -ql golang | grep include
/usr/lib/golang/include
(This is for Fedora 20)
the values should be (MACOS):
GOROOT="/usr/local/go" --> all binaries file core go
GOPATH="/Applications/proyectos/go" --> the route to workspace (custom workspace)

How do I install requirements in Go? "cannot find package"

I'm new to Go and I'm trying to set up a Go project with minimal documentation: https://github.com/alphagov/metadata-api
I've cloned it, but when I try go build I get the following warnings:
main.go:8:2: cannot find package "github.com/Sirupsen/logrus" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/Sirupsen/logrus (from $GOROOT)
/Users/me/go/src/github.com/Sirupsen/logrus (from $GOPATH)
main.go:14:2: cannot find package "github.com/alphagov/metadata-api/content_api" in any of:
/usr/local/Cellar/go/1.3.3/libexec/src/pkg/github.com/alphagov/metadata-api/content_api (from $GOROOT)
/Users/me/go/src/github.com/alphagov/metadata-api/content_api (from $GOPATH)
I'm guessing this is because I haven't installed the Go equivalent of requirements?
My GOPATH is set:
metadata-api$ echo $GOPATH
/Users/me/go
And the Go executable is in
metadata-ape$ echo $PATH
....:/Users/me/go/bin
What do I need to do to help Go find these packages?
You should install package first:
try
$ go get github.com/Sirupsen/logrus
and check you $GOPATH dir
This project use gom as the package manager,
Make sure you have installed gom
or try this command
$ gom install
I think your $GOPATH and $PATH settings are incorrect, the $GOPATH environment variable specifies the location of your workspace, these are my path settings:
export GOROOT=$HOME/bin/go
export GOBIN=$GOROOT/bin
export GOPATH=$HOME/golang
export PATH=$PATH:$GOBIN
I had similar issue and
export GO111MODULE=on
helped.
When you need your code to do something that might have been implemented by someone else (in Github or a package somewhere else), You should initialize a go mod file inside of your folder.)
For the purposes of this example, I'll just use example.com/module.
go mod init example.com/module
Add new module requirements and sums:
go mod tidy
Run your program:
go run .
For more details, see https://golang.org/doc/tutorial/getting-started.
Was able to fix the similar issue in Go 1.13.7 by typing:
export GOPATH=~/go
go get github.com/profile/repository
(e.g. github.com/Sirupsen/logrus)
"...Starting in Go 1.13, module mode will be the default for all development..."
"...When using modules, GOPATH is no longer used for resolving imports. However, it is still used to store downloaded source code (in GOPATH/pkg/mod) and compiled commands (in GOPATH/bin)..."

What should be the values of GOPATH and GOROOT?

I'm trying to install doozer like this:
$ goinstall github.com/ha/doozer
I get these errors.
goinstall: os: go/build: package could not be found locally
goinstall: fmt: go/build: package could not be found locally
goinstall: io: go/build: package could not be found locally
goinstall: reflect: go/build: package could not be found locally
goinstall: math: go/build: package could not be found locally
goinstall: rand: go/build: package could not be found locally
goinstall: url: go/build: package could not be found locally
goinstall: net: go/build: package could not be found locally
goinstall: sync: go/build: package could not be found locally
goinstall: runtime: go/build: package could not be found locally
goinstall: strings: go/build: package could not be found locally
goinstall: sort: go/build: package could not be found locally
goinstall: strconv: go/build: package could not be found locally
goinstall: bytes: go/build: package could not be found locally
goinstall: log: go/build: package could not be found locally
goinstall: encoding/binary: go/build: package could not be found locally
GOPATH is discussed in the cmd/go documentation:
The GOPATH environment variable lists places to look for Go code. On
Unix, the value is a colon-separated string. On Windows, the value is
a semicolon-separated string. On Plan 9, the value is a list.
GOPATH must be set to get, build and install packages outside the
standard Go tree.
GOROOT is discussed in the installation instructions:
The Go binary distributions assume they will be installed in
/usr/local/go (or c:\Go under Windows), but it is possible to install
the Go tools to a different location. In this case you must set the
GOROOT environment variable to point to the directory in which it was
installed.
For example, if you installed Go to your home directory you should add
the following commands to $HOME/.profile:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
Note: GOROOT must be set only when installing to a custom location.
(updated version of Chris Bunch's answer.)
Here is a my simple setup:
directory for go related things: ~/programming/go
directory for go compiler/tools: ~/programming/go/go-1.4
directory for go software : ~/programming/go/packages
GOROOT, GOPATH, PATH are set as following:
export GOROOT=/home/user/programming/go/go-1.4
export GOPATH=/home/user/programming/go/packages
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
So, in short:
GOROOT is for compiler/tools that comes from go installation.
GOPATH is for your own go projects / 3rd party libraries (downloaded with "go get").
First run go env.
If you see that the go isn't installed, you can install it via homebrew or via package and/or other ways.
If you are seeing output then your Go is installed.
It shows you all the envs that are set and are not.
If you see empty for GOROOT:
Run which go (On my computer : /usr/local/go/bin/go)
then export like this export GOROOT=/usr/local/go
If you see empty for GOPATH:
Create any directory anywhere on your computer for go projects in my case: ~/GO_PROJECTS
Then export GOPATH=~/GO_PROJECTS
GOPATH is discussed here:
The GOPATH Environment Variable
GOPATH may be set to a colon-separated list of paths inside which Go
code, package objects, and executables may be found.
Set a GOPATH to use goinstall to build and install your own code and
external libraries outside of the Go tree (and to avoid writing
Makefiles).
And GOROOT is discussed here:
$GOROOT The root of the Go tree, often $HOME/go. This defaults to the
parent of the directory where all.bash is run. If you choose not to
set $GOROOT, you must run gomake instead of make or gmake when
developing Go programs using the conventional makefiles.
I read the go help gopath docs and was still incredibly confused, but found this little nugget from another go doc page:
The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code.
http://golang.org/doc/code.html#GOPATH
Starting with go 1.8 (Q2 2017), GOPATH will be set for you by default to $HOME/go
See issue 17262 and Rob Pike's comment:
$HOME/go it will be.
There is no single best answer but this is short and sweet, and it can only be a problem to choose that name if $HOME/go already exists, which will only happen for experts who already have go installed and will understand GOPATH.
The GOPATH should not point to the Go installation, but rather to your workspace (see https://golang.org/doc/code.html#GOPATH). Whenever you install some package with go get or go install, it will land within the GOPATH. That is why it warns you, that you most definitely do not want random packages from the internet to be dumped into your official installation.
You generally should not set GOROOT explicitly. The go command identifies the appropriate GOROOT automatically based on its own directory location.
GOPATH defaults to $HOME/go. You only need to set it explicitly if you want to put it somewhere else.
GOPATH contains:
Binaries installed using go install, located at $GOPATH/bin.¹
This location can be overridden using the GOBIN environment variable.
A cache of downloaded module source code and checksums, located at $GOPATH/pkg/mod.
This location can be overridden using the GOMODCACHE environment variable.
If you set both GOBIN and GOMODCACHE, and do not set GO111MODULE=off, then GOPATH itself should have essentially no effect.
In addition, in the legacy GOPATH mode (when GO111MODULE=off is also set), GOPATH contains:
Source code used to build packages, stored in a directory tree rooted at $GOPATH/src.
Non-binaries installed using go install, located at $GOPATH/pkg.
Installing non-binary packages is no longer particularly useful: the go command has a cache of built artifacts, which has been required since Go 1.12 even in GOPATH mode.
The build cache is not located within GOPATH. Its location can be set with the GOCACHE environment variable.
¹ Binaries can also be installed using go get on Go 1.17 and earlier, but go install is preferred as of Go 1.16; see https://golang.org/doc/go1.16.
Regarding GOROOT specifically, Go 1.9 will set it automatically to its installation path.
Even if you have multiple Go installed, calling the 1.9.x one will set GOROOT to /path/to/go/1.9 (before, if not set, it assumed a default path like /usr/local/go or c:\Go).
See CL Go Review 53370:
The go tool will now use the path from which it was invoked to attempt to locate the root of the Go install tree.
This means that if the entire Go installation is moved to a new location, the go tool should continue to work as usual.
This may be overriden by setting GOROOT in the environment, which should only be done in unusual circumstances.
Note that this does not affect the result of the runtime.GOROOT() function, which will continue to report the original installation location; this may be fixed in later releases.
In modern Go, you don't need to set GOPATH or GOROOT. In fact, unless you're doing something very specialized, it's best to have them unset on your system.
Use Go modules. Having installed Go, pick a directory where you want to work. Then:
$ mkdir example
$ cd example
$ go mod init example.com
Note that the module name example.com is arbitrary; if you keep your work on GitHub, this could be something like github.com/your-username/project-name.
The last command will have created a go.mod file; now you can grab dependencies with go get:
$ go get rsc.io/quote
Now your code using this dependency:
$ touch main.go
Place this in main.go:
package main
import (
"fmt"
"rsc.io/quote"
)
func main() {
fmt.Println(quote.Go())
}
And run with:
$ go run .
W.r.t. original question, you can now get your doozer dependency with:
$ go get github.com/ha/doozer
Now you can use the doozer module in your code. And so on. You can also examine the go.mod file in your directory to see the dependencies listed, along with their versions. Each module is self-contained, with its own versions of dependencies. You can have two modules alongside each other, each with its own go.mod file pointing to different versions of some dependency - this will all work OK because of the isolation between modules.
For additional information, start with the official tutorial here. In several chapters, it walks you through the steps shown above, as well as writing your own reusable modules and packages, and importing them from other modules. Additional interactive tutorials are available at https://play-with-go.dev/
GOPATH and GOROOT configurations are deprecated.
You can use the GO module instead.
For example:
mkdir go_app
cd go_app
go mod init go_app
As mentioned above:
The GOPATH environment variable specifies the location of your
workspace.
For Windows, this worked for me (in Ms-dos window):
set GOPATH=D:\my_folder_for_go_code\
This creates a GOPATH variable that Ms-dos recognizes when used as follows:
cd %GOPATH%
Lots of answers but no substance, like robots doing cut and paste on what's on their system. There is no need to set GOROOT as an environment variable. However, there is a beneficial need to set the GOPATH environment variable, and if not set it defaults to ${HOME}/go/ folder.
It is the PATH environment variable that you must pay attention because this variable is the variable that can change your go version. Not GOROOT! Forget GOROOT.
Now, if you switch or change to a new go version, your downloaded packages will use the default $HOME/go folder and it will mixed-up with whatever your previous go version was. This is not good.
Therefore, this is where GOPATH you need to define in order to isolate downloaded packages of the new go version.
In summary, forget GOROOT. Think more on GOPATH.
Run go help environment it has documentation for every environment variable that can be listed by go env command
Here is one solution (single user):
GOROOT=$HOME/.local # your go executable is in $GOROOT/bin
GOPATH=$HOME/.gopath
PATH=$GOROOT/bin:$GOPATH/bin:$PATH
go complains if you change .gopath to .go.
I wish they went with how the rust/cargo guys did and just put everything at one place.
There's a command you can use: go env GOPATH
You don't need to explicitly set GOROOT (Modern versions of Go can figure it out on their own based on the location of the go binary that you run).
Also, got the follow error when trying to work with vgo:
go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'
Removing GOROOT, updating my GOPATH and export GO111MODULE="on" resolved the issue.
GOPATH see in here
GOPATH may be set to a colon-separated list of paths inside which Go code, package objects, and executables may be found.
Set a GOPATH to use goinstall to build and install your own code and external libraries outside of the Go tree (and to avoid writing Makefiles).
As of 2020 and Go version 1.13+, in Windows the best way for updating GOPATH is just typing in command prompt:
setx GOPATH C:\mynewgopath
I had to append
export GOROOT=/usr/local/Cellar/go/1.10.1/libexec
to my ~/.bash_profile on Mac OS X
There is also a case where when we use go it compiles all the go files.
So lets say we had one file main.go and later we changed the current file to main_old.go and then added our new main.go file. Then when we build our app all the go files will get compiled. So the error that's happening might be due to compilation error in some other go files.
Once Go lang is installed, GOROOT is the root directory of the installation.
When I exploded Go Lang binary in Windows C:\ directory, my GOROOT should be C:\go.
If Installed with Windows installer, it may be C:\Program Files\go (or C:\Program Files (x86)\go, for 64-bit packages)
GOROOT = C:\go
while my GOPATH is location of Go lang source code or workspace.
If my Go lang source code is located at C:\Users\\GO_Workspace, your GOPATH would be as below:
GOPATH = C:\Users\<xyz>\GO_Workspace
For all newcomers they could do a simply export GOPATH=$HOME/go if you are using Ubuntu or do go help gopath for more information.
in osx, i installed with brew, here is the setting that works for me
GOPATH="$HOME/my_go_work_space" //make sure you have this folder created
GOROOT="/usr/local/Cellar/go/1.10/libexec"
If you are using the distro go, you should point to where the include files are, for example:
$ rpm -ql golang | grep include
/usr/lib/golang/include
(This is for Fedora 20)
the values should be (MACOS):
GOROOT="/usr/local/go" --> all binaries file core go
GOPATH="/Applications/proyectos/go" --> the route to workspace (custom workspace)

Resources