Import "github.com/abcd/xyz" is a program, not an importable package.

I am trying to execute “GO TEST” in a folder where source code and test files are present.
I am using ginkgo here.

I found few people facing this error because “a non-main package cant import main package”.
i made both as main package. Still getting this same error

What did you expect to see?
Result of Test cases

What did you see instead?
import “github.com/abcd/xyz” is a program, not an importable package.

So github.com/abcd/xyz is a main package? This might be the reason for the error message then. What happens if you change the name of the imported package from main to xyz?

If i change the name of imported package, it is showing as "can’t load package: package .: found packages: XYZ and abcd ".

and i am not importing "package . ". So little confused why it is throwing error.

XYZ is the changed name here.

1 Like

In the import statement, did u tried removing dot(.) before github

1 Like

I tried that also. But once i removed the “.” before the github and save the file. The complete import statement getting erased.

1 Like

Which editor r u using?. I am using jetbrains btw, and in that there is no preceding dot. Did u tried running in other editors too?

1 Like

I am using in visual studio.
I will try this in jetbrains.

1 Like

I tried with jetbrains.
Got same error even after removing the “.” before the package name in the IMPORT module.
Error :
can’t load package: package .: found packages Xyz and abcd

1 Like

What is ur gopath set to?

1 Like

GOPATH=C:\Users\abcd\go

1 Like

Is path to ur package C:\Users\abcd\go\src/github.com/abcd/xyz correct?

1 Like

Yes. Its correct.

1 Like

In the above image, package main is underlined red. R u using func main() in ur program bcoz if not then it might be throwing an error. Can u try showing the code?.

1 Like

Yes. I am not using func main() here.
This is a test file. i am using ginkgo in the code. So there is no func main() in the code.
Should i change the package name to something else ?

1 Like

That’s the reason u r getting an error. Check this link :- http://goxample.blogspot.in/2012/09/the-go-programming-language.html

When you are declaring your main function should be declared under package main otherwise you will get error: go run: cannot run non-main package

1 Like

Thanks for that article.
I changed the package name to xyz. Still facing same error “can’t load package: package .: found packages abcd”.
in Jetbrains, i got few errors in the code. I rectified all the errors.

1 Like

You mentioned you have no main() function in your file because it is a test file.

By convention, a file that contains Go test code must end in _test.go. For example, xyz_test.go if it contains unit tests for package xyz. The Go compiler ignores _test.go files whereas the go test tool specifically looks for these files.

But still, if your test functions reside within package main, you need a non-test file for package main that does contain a main() function. For example,

  • main.go contains main()
  • main_test.go contains unit tests for main.go (but no main() function).

If you want to write unit tests for a non-main package, put your tests under that package instead of main. (For example, to write unit tests for code in package xyz, you would have a file somecode.go and
a file somecode_test.go, where both files start with package xyz and reside in the directory named xyz.)
(Disclaimer: I don’t know how Ginkgo works, maybe it has its own rules. But if Ginkgo does not integrate well with the standard go test tool, I would stay away from it.)


A side note on the dot import:

A dot that precedes an import path says that the imported package’s namespace gets merged with the current namespace. For example, if you do this:

import . "fmt"

then you can write

Printf(...)

instead of

fmt.Printf(...)

Usually, this kind of import is a bad idea as it pollutes the current namespace and makes it hard to track down the origin of a given function, type, or variable. (Consider that other people may look at the code, too, and they might be confused by seeing calls to seemingly local functions that are not defined in the current package. Using the package prefix like in fmt.Printf() does improve code comprehension a lot.)

You mentioned that removing the dot makes the import paths vanish. This happens when an imported package is not used in the current code. In the “fmt” example above, the current package would not contain any calls preceded by “fmt.” (even though the unprefixed “Printf()” calls are still there), and so the relevant tool (usually goimports or goreturns, which are typically triggered from a Go-aware editor when saving a Go file) removes the seemingly unused imports. (If they are not removed, the compiler would error out, saying this import is not used. This ensures that source code only contains required imports, in order to avoid slowing down compilation needlessly.)

3 Likes

@christophberger Thanks for the detailed explanation.:blush:

As of now ,
i have this structure

Under “CODE” folder, i have
code.go ------------contains main() [package main]
code_test.go — contains unit tests for code.go (but no main() function) [package main_test ]

I have code " import ( . “github.com/abcd/code” ) " in code_test.go file.

In this,
Error : import “github/abcd/code” is a program, not an importable package.

You Said
"If you want to write unit tests for a non-main package, put your tests under that package instead of main. (For example, to write unit tests for code in package xyz, you would have a file somecode.go and
a file somecode_test.go, where both files start with package xyz and reside in the directory named xyz.)"

In this case you mean :
Code : -------somecode.go [package xyz ]
Test code : -somecode_test.go [package xyz]
and both files should be in XYZ directory.

I tried this.
Error : can’t load package: package .: found packages main_test (somecode.go) and main (somecode_suite_test.go) in C:\Users\abcd\Documents\golang\main_test

here i dont know why it is showing package “.”

1 Like

You don’t need this import. If the test code resides in package main, it already has access to all code in package main.

2 Likes

Even if i remove the import. I am getting same error as "can’t load package: package .: found package main_test (main_test.go) "