How to call a function from another file in Go: project structure and import
Hello, colleagues! I just started learning Go and encountered a problem that seems very simple to me, but I can't find the right solution. I have two files in one project: `main.go` and `helpers.go`. In `helpers.go`, I declared a function `sayHello()` that prints a greeting. In `main.go`, I'm trying to call this function, but the compiler complains that the function is not defined.
Here is my folder structure:
```
myproject/
├── main.go
└── helpers.go
```
Code for `helpers.go`:
```go
package main
import "fmt"
func sayHello() {
fmt.Println("Hello from helpers!")
}
```
Code for `main.go`:
```go
package main
func main() {
sayHello()
}
```
When I run `go run main.go`, I get an error: `undefined: sayHello`. If I run `go run main.go helpers.go`, then it
No answers yet.
Log in to leave an answer