title: How to structure imports in Golang test files
body: <p>I have functions defined in <code>main.go</code> that aren't accessible in <code>main_test.go</code>.  However, in some online tutorials, I see that functions are accessible: I would like to understand the difference, as well as how to structure these in a go-idiomatic way .</p>
  
  <p>Specifically, I have an application with multiple binaries:</p>
  
  <pre><code>myapp/cmd/app/main.go
  myapp/cmd/cli/main.go
  </code></pre>
  
  <p>I currently have a lot of simple unit test logic I run in the <code>func main</code> of the <code>main.go</code> files. I'm just testing individual functions and having them print their output, I'm not trying to invoke the whole power of the "testing" suite, if I can avoid it.</p>
  
  <p>Since my tests in the top of my main.go are so long at this point, I would like to move them to specific test files, in for example:</p>
  
  <pre><code>myapp/cmd/app/main_test.go
  myapp/cmd/cli/main_test.go
  </code></pre>
  
  <p>This works well enough with my makefile:</p>
  
  <pre><code># Makefile
  
  all: test build
  build:
      go build -o cmd/app/main cmd/app/main.go
      go build -o cmd/cli/main cmd/cli/main.go
  test:
      go test ./cmd/app/main_test.go
      go test ./cmd/cli/main_test.go
  </code></pre>
  
  <p>If I just want to run my testfiles, I'd like to take the item in my <code>app/main.go</code></p>
  
  <pre><code>// cmd/app/main.go
  package main
  
  func definedInMain(m string) string {
    // 
  }
  
  func main() {
    m := definedInMain("1")
    fmt.Println(m)
    // all the rest of my app's logic...
  }
  </code></pre>
  
  <p>And run it in my <code>main_test.go</code></p>
  
  <pre><code>// cmd/app/main_test.go
  
  package main
  
  // no imports, I hope?
  
  func testDefinedInMain() {
    m := definedInMain("1")
    fmt.Println(m)  
  }
  </code></pre>
  
  <p>However, this fails with:</p>
  
  <pre><code>undefined: definedInMain
  FAIL
  </code></pre>
  
  <p>I am confused that I have to import all these functions into my <code>main_test.go</code> files (and even when I try, it suggests <code>"can't load package: ... myapp/cmd/app/main" in any of: ..."</code>)</p>
  
  <p>Is there a clean and go-idiomatic way for me to test my very simple unit tests in test files  and run my functions in main files without a lot of rewriting imports or other substantial re-architecting?  </p>
  
  <p>From some links, I had the impression if I made a <code>main_test.go</code>, the imports would follow on their own (as they seem to in these examples).</p>
  
  <ul>
  <li><a href="https://medium.com/@thejasbabu/testing-in-golang-c378b351002d" rel="nofollow noreferrer">https://medium.com/@thejasbabu/testing-in-golang-c378b351002d</a>
  (where the Reverse function is not explicitly imported in reverse_test.go)</li>
  <li><a href="https://tutorialedge.net/golang/intro-testing-in-go/" rel="nofollow noreferrer">https://tutorialedge.net/golang/intro-testing-in-go/</a>
  (where the Calculate function is not explicitly imported in main_test.go)</li>
  <li><a href="https://blog.alexellis.io/golang-writing-unit-tests/" rel="nofollow noreferrer">https://blog.alexellis.io/golang-writing-unit-tests/</a>
  (where the Sum function is not explicitly imported in main_test.go)</li>
  </ul>
  
  <p>So, you can see I am a bit confused tutorials get their functions imported for free, and I do not, and I just want to understand the magic better.  </p>
  
  <p>Is it just the case my function, definedInMain, is private because it's lowercased?  I do actually want this function to be private, but I'd expect it to be exported to the main_test.go file the same way it is usable in main.go.  Do I have to make it fully public to have tests in another file?  That doesn't seem correct, eg, "it is only possible to unit test public methods in go?"</p>