title: Why does the "go run" command fail to find a second file in the main package?
body: <p>I am experiencing an issue where <code>go run main.go</code> produces the error:</p>
  <pre><code># command-line-arguments
  ./main.go:9: undefined: test
  </code></pre>
  <p>However the commands <code>go build &amp;&amp; ./goruntest</code> compile and run the program just fine.</p>
  <p>The output is:</p>
  <blockquote>
  <p>Hi from test()</p>
  <p>Hi from sameFileTest()</p>
  <p>Hi from pkgtest.Test()</p>
  <p>Hi from pkgtest.Test1()</p>
  </blockquote>
  <p>I have the directory set-up like so:</p>
  <pre><code>go/src/github.com/username/goruntest/
      pkgtest/
          pkgtest.go
          pkgtest1.go
      main.go
      test2.go
  </code></pre>
  <p>Here is the code.</p>
  <p><strong>main.go</strong></p>
  <pre><code>package main
  
  import (
      &quot;fmt&quot;
      &quot;github.com/username/goruntest/pkgtest&quot;
  )
  
  func main() {
      fmt.Println(test())           // main.go:9
      fmt.Println(sameFileTest())
      fmt.Println(pkgtest.Test())
      fmt.Println(pkgtest.Test1())
  }
  
  func sameFileTest() string {
      return &quot;Hi from sameFileTest()&quot;
  }
  </code></pre>
  <p><strong>gotest1.go</strong></p>
  <pre><code>package main
  
  func test() string {
      return &quot;Hi from test()&quot;
  }
  </code></pre>
  <p><strong>pkgtest/pkgtest.go</strong></p>
  <pre><code>package pkgtest
  
  func Test() string {
      return &quot;Hi from pkgtest.Test()&quot;
  }
  </code></pre>
  <p><strong>pkgtest/pkgtest1.go</strong></p>
  <pre><code>package pkgtest
  
  func Test1() string {
      return &quot;Hi from pkgtest.Test1()&quot;
  }
  </code></pre>
  <p>I understand that the problem is the second file as part of <code>package main</code> and I also understand that there is no real reason to have a second file in <code>main</code>.</p>
  <p><strong>My question is:</strong> Why is <code>go run</code> unable to handle this set-up but building and running the executable works just fine?</p>
  <p><strong>EDIT</strong></p>
  <p>Included a second file in <code>pkgtest</code></p>
  <p>I also understand that the command <code>go run main.go gotest1.go</code> works but why do I need to specify <code>gotest1.go</code>?</p>
  <p>I originally omitted these details for the sake of brevity. But now I see they are important to the question.</p>