Golang package is not in GOROOT (/usr/local/go/src/packageName) - go

Hi could someone help me run my main.go: go run main.go ?
There are two folders, which are next to each other:
proj1 folder has main.go, go.mod
package1 folder has package1.go, go.mod, utility.go
inside of main.go:
package main
import (
"package1"
"fmt"
)
func main() {
y := package1.Struct1{
v: "1",
}
z := package1.isTrue()
fmt.Println(z)
}
inside my package folder: package1.go
package package1
type Package1 struct {}
func (a *Package1) IsTrue() bool {
return true
}
My Go version: go1.15.2 linux/amd64
My Go env settings:
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user1/.cache/go-build"
GOENV="/home/user1/.config/go/env"
GOMODCACHE="/mnt/sda5/gopath/pkg/mod"
GOOS="linux"
GOPATH="/mnt/sda5/gopath"
GOROOT="/usr/local/go"
...
I tried :
go install, go build ... results no error inside the package folder
go mod vendor, go run main.go, go get -u package1 ... result in the same message when run inside the proj1 folder:
package package1 is not in GOROOT (/usr/local/go/src/package1)
The VS Code Go Plugin Linter shows no problem.
Please help. Thank you!

To solve the error i was facing package package1 is not in GOROOT (/usr/local/go/src/package1)
I had to ensure the environment variables were correctly configured.
I added those lines in the bashrc file:
export GO111MODULE=on
#GOPATH MUST BE OUTSIDE OF GOROOT directory!!!
export GOPATH=/mnt/sda1/programming/gopath
export PATH=$PATH:$GOPATH/bin
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
I loaded the bashrc file in the terminal:
source ~/.bashrc
Now i can execute following procedure to program with the Go language.
Make a new main folder...
Inside this main folder:
make main.go file begin with package main
Run the command below:
go mod init main
make another folder with the new package name: e.g. package1
inside the package1 folder:
make all files with package package1 in its 1st line ... but DO NOT MAKE MOD FILE inside this package folder!!!
in your main.go, you can import that package and use it
import "main/package1"
y := package1.Struct1{a: 1, b: 2,...}
z := y.func1()

This page is the top search result for...
package xxxxx is not in GOROOT (/usr/lib/go/src/xxxxx)
In my case, I was trying:
go build mything
That gave me the error. The solution was simple, I needed to be more specific:
go build mything.go

I faced the same problem but in my case i forgot the .go so after running :
problem : go run main
solution: go run main.go
and now it works fine

Related

GO (Golang) does not see other .go files inside the same dir

PROBLEM: functions exported from other packages are undefined, invisible from inside of main.go
SOLUTION:
Put main.go into a separate folder (name it app or main, doesn't matter). Only go.mod remains in root, with the folders "app" and "package1" "package2" etc. After this VSCode automatically added imports on save, I didn't even need to do anything. If I have main.go in root then it doesn't work (most of the time) as it doesn't "see" functions from other modules (undefined).
I found the solution HERE,(see post by davidbost). The solution on this page by Andrey Dyatlov worked too for a while and then stopped working.
It took me probably 10 hours of try and error and searching.
Hopefully the above will help others. Thank you, everyone!
_______________________________________________________________________
Original Problem Description:
Windows 10, Go 1.17, VS Code with Go extension.
Hello, I am new to Go and I was not able to follow a single tutorial due to the following issue. When I create another .go file in the same directory (or inside a folder of the same directory) as the main.go, I receive error saying .\main.go:7:2: undefined: SayHi
main.go file:
package main
import "fmt"
func main() {
fmt.Println("1st")
SayHi()
}
another .go file inside /something folder:
package something
import "fmt"
func SayHi() {
fmt.Println("Hi!")
}
Running go run main.go results in the undefined SayHi error
I googled the issue with no luck.
$ go build // gives out the same error
$ go install // gives out the same error
without using functions from other files $ go run main.go runs just fine.
I also tried go init with adding my github directory with no luck (by following a tutorial). I also tried the official starting guide with go run init, and the exported Capitalized function is still undefined. What's worse is that autocomplete for the SayHi function works, yet it won't compile because undefined.
I have set PATH to C:\Users\xxx\go and put my files there, I also tried using C:\Go with no luck, it's still undefined. I'm about to give up on Go...
It' would be hard to fix your project without knowing its current state. Please, try to start from scratch:
Create a directory for your project anywhere outside $GOPATH:
mkdir myproject
cd myproject
Run the following command to create a go.mode file that describes your project (module) and its dependencies; let's call the module github.com/me/myproject:
go mod init github.com/me/myproject
Create the first file; let's call it main.go:
package main
import "fmt"
import "github.com/me/myproject/something"
func main() {
fmt.Println("1st")
something.SayHi()
}
Create a directory for the package called github.com/me/myproject/something:
mkdir something
Create a file with path something/something.go:
package something
import "fmt"
func SayHi() {
fmt.Println("Hi!")
}
From the myproject directory, run go build.
Launch your first Go program:
./myproject
1st
Hi!

How do I fix VS Code error: "Not able to determine import path of current package by using cwd" for Go project

I'm following tutorials and I think I may have missed something.
I have a Go project located at:
/Users/just_me/development/testing/golang/example_server
The contents are:
main.go
package main
import "fmt"
func main() {
fmt.Println("hi world")
}
I have a ~/go directory.
go env shows:
GOPATH="/Users/just_me/go"
GOROOT="/usr/local/Cellar/go/1.12.9/libexec"
I installed the suggested packages in VSCode.
When I save my main.go I get:
Not able to determine import path of current package by using cwd: /Users/just_me/development/testing/golang/example_server and Go workspace:
/Users/just_me/development/testing/golang/example_server>
How do I fix this?
Since your package is outside of $GOPATH, you may need to create a module file.
You'll need to init your go module using
go mod init your.import/path
Change the import path to whatever you like.
This way you set the import path explicitly, which might help fix it.
The resulting go.mod file looks like this:
module your.import/path
go 1.14 // Your go version
So if the go.mod file is in the same directory as a main.go file, you can now import submodules from it:
E.g. main.go:
package main
import (
"your.import/path/somepackage" // Import package from a subdirectory. This only works if `go.mod` has been created as above
)
func main() {
somepackage.SomeMethod()
}
And in somepackage/whatever.go:
package somepackage
import "fmt"
func SomeMethod() {
fmt.Println("Success!")
}
if you are using vs code, check if the go and code runner extensions are enabled, if enabled, try disabling and enabling again, and if not, install and enable, and download all requested packages.

Goland doesn't see functions from another file

I have main.go file
package main
func main() {
func2()
}
func2 is defined in file2.go:
package main
func func2(){
...
}
Everything is OK when I compile and run it from a command line:
go run main.go file2.go
But when I run it from Goland by pressing RUN it gives me an error:
# command-line-arguments
./main.go:95: undefined: func2
How should I overcome it?
Go to Run | Edit Configurations and change the run configuration Type to point from File to a Package, then the package name must be fully qualified (for example github.com/user/package).
Edit:
You can also right-click on the folder and then select Run | go test <folder name>. This will create a run configuration of the type Directory.
Note: there are differences between the Directory and Package type configurations.

Does it make sense to have two packages in the same directory?

I have a project that provides a library (exports some funcs) and also must provide a command-line interface (there must be an executable file).
Example of directory structure:
whatever.io/
myproject/
main.go
myproject.go
The go compiler needs the package main and func main to start execution. My library needs the package myproject where I put stuff on it. This is what the go tool says when I am building another project that tries to import myproject:
main.go:5:2: found packages myproject (myproject.go) and main (main.go) in $GOPATH/src/whatever.io/myproject
So I believe there is no way to do it.
Should I move the library or the CLI to another package?
Just move your packages inside a new folder within the same directory of main.go.
Remember to import the new package from the reference of the $GOPATH.
Example:
user#user:~/p/go/test/so-multipack$ ls -R
.:
a main.go
./a:
a.go
user#user:~/p/go/test/so-multipack$ cat main.go
package main
import (
"../so-multipack/a"
)
func main(){
a.Hello()
}
user#user:~/p/go/test/so-multipack$ cat a/a.go
package a
import (
"fmt"
)
func Hello(){
fmt.Println("hello from a")
}
user#user:~/p/go/test/so-multipack$ go run main.go
hello from a
user#user:~/p/go/test/so-multipack$ go build
user#user:~/p/go/test/so-multipack$ ls
a main.go so-multipack
user#user:~/p/go/test/so-multipack$
Useful link:
go build vs go build file.go
You cannot have two packages per directory, hence the error. So the solution as #Larry Battle said to move your myproject.go to a new directory.
From How to write go code
Go code must be kept inside a workspace. A workspace is a directory
hierarchy with three directories at its root:
src contains Go source files organized into packages (one package per directory),
pkg contains package objects, and
bin contains executable commands.
In most cases, no. However, there is an exception for unit tests.
Working Example:
Here are 2 different packages (mypackage and mypackage_test) in 1 directory (mypackage). The compiler will not complain about this.
mypackage folder:
mypackage/
foo.go
foo_test.go
mypackage/foo.go:
package mypackage
func Add(a int, b int) int {
return a + b
}
mypackage/foo_test.go:
package mypackage_test
// Unit tests...
Rules:
The 2 packages must have the following names:
NameOfDirectory.
NameOfDirectory + _test.
The names of the files in the _test package must end with _test.go
If you're receiving a confusing compiler error along the lines of found packages "foo" and "bar", you've probably broken one or more of these rules.
You can't have two golang files in one directory with two packages. So you need to move main.go out of myproject.
the directory structure before move
whatever.io/
go.mod
myproject/
main.go
myproject.go
After move
whatever.io/
go.mod
main.go
myproject/
myproject.go
And you need to change your main.go's import path. If the module name is aaa
Before
import "aaa"
Need change to this
import "aaa/myproject"

cannot compile a test program in Go

compiler: http://code.google.com/p/go/downloads/detail?name=go1.0.3.windows-386.zip&can=2&q=
I've unpacked it to d:\, then made another directory d:\testgo, where I put two files:
The code:
package main
import "fmt"
func main() {
fmt.Println("Hello world!")
}
and the CMD file to run the compilation:
SET PATH=%PATH%;D:\go\bin
go build test.go
#pause>nul
And I got no exe but this:
test.go:3:8: import "fmt": cannot find package
package runtime: import "runtime": cannot find package
So what went wrong ?
The documentations says the following:
If you chose a directory other than c:\Go, you must set the GOROOT
environment variable to your chosen path.
SET GOROOT=d:\Go should do it.

Resources