Search code examples
gogo-packages

Unable to import func A() of package main, inside another package main func main. There are 2 package main


At package level main I have 2 files hello.go and main.go.

|- hello.go
|- main.go

Both the files are in package main level but unlike other packages I am unable to import a func defined in the hello in the func main. Can there be only 1 file with package main?

// hello.go
package main

import "fmt"

func Hello() {
  fmt.Println("hello world")
}

// main.go
package main 

func main() {
  Hello()
}

Error

./main.go:4:2: undefined: Hello

Solution

  • In the terminal, you should use

    go run .
    

    instead of

    go run main.go