-3

In the below code:

../folder1/some_test.go

package main_test

import "testing"

func TestF(t *testing.T) {
    main.F()
}

../folder1/some_file.go

package main

func F() {

}

main.F() gives undefined main

$ go version
go version go1.14.3 linux/amd64

Renaming package name from main_test to main resolve the problem.

Why main_test package name is not allowed for testing code, in same folder?

overexchange
  • 15,768
  • 30
  • 152
  • 347

2 Answers2

0

You must import the package to use the package.

go.mod:

module example.app

main.go:

package main

func F() {}

func main() {}

main_test.go

package main_test

import (
    "testing"
    "example.app"  // import the main package
)

func TestF(t *testing.T) {
    main.F()
}

The code above assumes that main*.go are in the same directory as go.mod for package example.app. Replace example.app with the name of your module. Adjust the paths to match your application. For example, if the the main*.go files are in the directory cmd/example below the directory containing go.mod, use the import path example.app/cmd/example instead of example.app.

Note that a test can import a main package this way, but non-test code cannot.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
-3

The problem is that main_test is a different package from main.

To access functions in main you need to import main and access the functions like this: main.F()

Also, note f starts with lowercase and thus is not exported from the package main. To access it in main_test it needs to be exported (which can be done by changing it to start with a capital letter: F).

Alternatively, you can change the test file to be in package main.

Edit with a note: When importing main note that import paths are by directory name. Usually developers in Go put their packages in directories with the same name as the package (e.g. put main in a directory named main). In your case the package and directory names are different so the import will be import ".../folder1" not import ".../main". You'll still be able to use main.F() to access the function:

package main_test

import (
   "testing"
   "../folder1" // use the full path to folder1 from the root of your module
)

func TestF(t *testing.T) {
    main.F()
}
phonaputer
  • 1,485
  • 4
  • 9
  • I changed it to `F()` I still see the same error. Query edited – overexchange Jul 10 '20 at 00:58
  • Are you importing `main`? – phonaputer Jul 10 '20 at 01:00
  • If I import `main` then it gives error for line `package main_test` now with error `can't load package: package main:` – overexchange Jul 10 '20 at 01:01
  • Running command `go test some_test.go` from folder `../folder1` and the error is: `imports main: cannot find package "main" in any of` GOROOT, GOPATH – overexchange Jul 10 '20 at 01:06
  • Looks like your import path is wrong. If you're using Go modules, import main using the path starting with the root directory of your module. If not, import it with the file path from $GOPATH/src – phonaputer Jul 10 '20 at 01:09
  • For example, if your project's root dir is "myproject" and the package `main` is in "folder1" you can import it like `import "myproject/folder1"` in which case you will still do `main.F()`. – phonaputer Jul 10 '20 at 01:17
  • `some_test.go` works with `package main` without any error – overexchange Jul 10 '20 at 01:20
  • That makes sense - because now it is in the same package as `F` so no importing is needed. – phonaputer Jul 10 '20 at 01:21
  • But Go allows `packagename_test` package name in same folder, Isn't it? – overexchange Jul 10 '20 at 01:24
  • 1
    That is correct. But, even though it's in the same directory, it is a different package. And thus `main` needs to be imported. Also, just to add - import statements are by directory name not package name so I think an issue with your import might have been importing `main` instead of the directory in which `main` is located. Usually developers name their directory the same as the package contained in it, but from your post it appears the dir is not named `main` for you. – phonaputer Jul 10 '20 at 01:27
  • This [answer](https://stackoverflow.com/a/55105170/3317808) does not import, but yes, package name is same as folder – overexchange Jul 10 '20 at 01:30
  • 1
    I should mention `main "github.com/myhub/folder1"`. this works – overexchange Jul 10 '20 at 01:34
  • That example doesn't show any tests just the package name. Check out [this answer](https://stackoverflow.com/questions/19998250/proper-package-naming-for-testing-with-the-go-language/31443271#31443271) – phonaputer Jul 10 '20 at 01:34