Using function from `main` package into main.go file

I have 2 files into the root directory: main.go and utils.go.

main.go file is:

package main

func main() {
   CustomPrint()
}

utils.go file:

package main

import "fmt"

func CustomPrint() {
   fmt.Println("example")
}

However, I get an error because customPrint is not recognized.

Is there any way to do that without create another package to store utils.go file?


It's Simple actually, When you try to run "go run main.go", You only execute the main.go file. Utils.go file is not executed together with it.

First you can just execute

go run

it will prompt you error.

As you can see that is expect list and multiple files instead of single file, "No go files listed"

The solution is you just need to execute both files

go run main.go utils.go

There you go problem solved!

it will behave as you expected.

Second option is simpler which you do not have to type so many files.

You can run

go run ./

it will executed every .go files in your current directory