[Solved]Cannot find paqckage after second *.go file is added to package

Greetings,

yesterday I started with go and created a package with a simple structure type in it, today looking through a libary on github I see that a structure type can be used in a different go file in the same package without declaring it anew.

So for testing purpose I create a new *.go file in the same package and write a print function that displays the structure type I created in a different *go file in the same package. Now trying to use this the compiler complains that he can’t find the package in goroot or gopath.

So I restart the whole in a new package put the structure type and the print function into a new *.go file in a different package and now trying to use it get the same error as before. Actually I now get this error on any package I create.

I read through the internet, did change environment variables and still this message pops up when compiling. So I’m a bit at loss.

Thanks for help in advance.

1 Like

Try to run it with go run ./…

(no spaces between the dots and slash)

1 Like

Perhaps show some code and a draft of how you have organized the files.

Also please be aware of the fact that package main is a bit special and usually does not automatically include other files.

1 Like

Well in gopath which is %user%/go/src I have a folder named own and in this a folder nanmed packtest with a file named test.go with following code:

package pt

type Test struct{
testword string
}

compiling this works, however when I for example write a hello.go in a hello folder in gopath with following code:

package main

import “own/packtest”

func main(){
}

the compiler doesn’t complain that I don’t use the import however the compiler complains:

hello.go:3:8: cannot find package “own/packtest” in any of:
GOROOT
GOPATH

Edit: did uninstall go and reinstall go, error message stays the same

1 Like

Don’t name your file like that. Filenames ending in test are ignored by the compiler unless you are running tests…

Also, when the compiler complains about beeing unable to find the package, compare carefully the actual path with the path in the error message.

1 Like

Okay, thanks for tip.

I created a folder called smt in src/own

compiler still puts out the same complaint when I use this one, compared the name, compied the filepath from console and corrected the backslash, complaint still appears.

1 Like

Can you provide a tarball or zip file containing a full example of the failing code?

1 Like

As you wish:

https://drive.google.com/file/d/1PFGljQC76C47BkU7KxnFlE2wUQFTFJT1/view?usp=sharing

I have all the files in the zip parked in gopath/src

1 Like

Okay weird but solved I copied the package own/smt into goroot/src and now it works. This was very confusing.

1 Like

DONT!

GOROOT is not for your stuff, thats for stuff contained in the go distribution!

You use GOPATH only!

Besides of that…

I unziped into my ${GOPATH}/src and go build hello3/hello.go “succeeded” with “imported and not used”…

1 Like

If I have the folder in gopath only then the compiler complains … no idea why go compiler in my windows makes this error appear.

1 Like

Perhaps your GOPATH isn’t what you think it is… Can you please provide the full error message, as well as the full path to your files?

And please remove the files from GOROOT, they do not belong there.

That is different from the default, the default for windows seems to be %USERPROFILE%/go. src is usually not part of the GOPATH, but one of three subfolders.

1 Like

The problem is the import path. It should be

import "packtest"

When packets are looked up, they are looked up relative to the main.go file. Since there is no ˋown` directory next to main.go, Go lookup in the GOPATH and GOROOT directory which fails.

EDIT: this is only true when the import path starts with either one or two dots. You can then use the following

import "./packtest"
1 Like

No, relative lookup is only used when you specify relative package patches starting with either one or two dots.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.