Goland doesn't see functions from another file - go

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.

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!

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

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

Difference between go run ./path/dir and go run path/dir

Note: This is not asking Why the functions defined in other "main" packages are not recognised?.
I've got a project with the following structure:
go/src/github.com/me/project/cmd/web/main.go
# main.go
package main
import(
"github.com/me/myproject/internal/myproject"
)
void main() {
// ...
}
go/src/github.com/me/project/cmd/web/handlers.go
# handlers.go
package main
void someFunc() {
// ...
}
And I compile and run successfully with:
go run ./cmd/web
I can also run successfully with go run cmd/web/*.go
However, when I try to run with go run cmd/web, why does compilation fail with the output:
package cmd/web: package cmd/web is not in GOROOT (/usr/local/go/src/cmd/web)
Why does ./cmd/web work, but cmd/web does not?
When you run ./cmd/web, you refer to the correct directory in your current directory tree.
cmd/web however refers to a package living where the stdlib packages are. If you try to run this example
go run net/http
It will tell you that it can't run it because its not a main package. It has found the stdlib net/http package with that name; but since cmd/web doesn't exist it doesn't work

Struct from the same parent folder as main is not visible

I have a small go demo project in Gogland with the structure:
awsomeProject
->src
->awsomeProject
->configuration.go
->main.go
Configuration file has a simple structure just for demo:
configuration.go:
package main
type Config struct {
Data int
}
Main file just uses the Config struct:
main.go
package main
import "fmt"
func main(){
var cfg Config
cfg.Data = 1
fmt.Println("lalala")
}
The error that I have is:
/usr/local/go/bin/go run /Users/lapetre/Work/awsomeProject/src/awsomeProject/main.go
command-line-arguments
src/awsomeProject/main.go:6: undefined: Config
Process finished with exit code 2
Any idea why the Config is not seen in main?
Thanks
When you build reusable pieces of code, you will develop a package as a shared library. But when you develop executable programs, you will use the package “main” for making the package as an executable program. The package “main” tells the Go compiler that the package should compile as an executable program instead of a shared library. The main function in the package “main” will be the entry point of our executable program.
That's why you should use the following structure:
awsomeProject
->src
->awsomeProject
->configuration.go
->main.go
with main.go
package main
import "fmt"
func main(){
var cfg awsomeProject.Config
cfg.Data = 1
fmt.Println("lalala")
}
and configuration.go
package awsomeProject
type Config struct {
Data int
}
For more details:
https://golang.org/doc/code.html
https://thenewstack.io/understanding-golang-packages
How are you calling go run? If you're calling it like
go run main.go
then that's the problem.
Go run only runs the file(s) you tell it to. So you need to tell it to also run configuration.go, or if you have several go files to run you can use
go run *.go
as eXMoor suggested.
There are some limits/drawbacks to "go run *.go" however, so the better alternative is to use go build instead of go run.
go build
Will compile everything. Then, to run the executable:
./awesomeProject
To combine all of this into one command that will compile and run whatever app you're working on, you can use:
go build && ./${PWD##*/}
I have it aliased to
gorunapp
just to make it easier.
This may not actually be the answer to the problem you're having, but hopefully you'll find it useful information regardless.
Try to run your application with command:
go run *.go

Why the functions defined in other "main" packages are not recognised?

I have to files main.go and main2.go . In main.go I have the main() function defined along with a call somefunc() which is in main2.go. The issue is that when I run go run main.go it says that somefunc() is undefined. Basically it doesn't scan the other main functions from package. However if I declare this somefunc() in main.go it works but when I run go test it says the function is redeclared.
Question: Is there any way that I can tell to go run to behave like go test and compile/run all the files from the package(in this case both main.go and main1.go) not just main.go?
You must include all the files as argument of the go run.
go run main1.go main.go
or
go *.go
Unless there are test files in the same folder.

Resources