Go not picked up my new file when newly created it

I’m new to Golang and currently I have some problems with Go were not automatically pickup my new file, follow by a structure:

parent
|_custompackage
|     |_customFile.go
|     |_go.mod
|_examplepackage
|     |_examplefile1.go
|     |_examplefile2.go
|     |_go.mod
|_mymain
      |_mainfile.go
      |_go.mod

For the go.mod, here is the content:
custompackage go.mod:

module username/GoLearn/custompackage

go 1.23.1

examplepackage go.mod:

module username/GoLearn/examplepackage

go 1.23.1

mainfile go.mod:

module username/GoLearn/mainfolder

go 1.23.1

replace username/GoLearn/custompackage => ../custompackage

replace username/GoLearn/examplepackage => ../examplepackage

require (
	username/GoLearn/custompackage v0.0.0-00010101000000-000000000000
	username/GoLearn/examplepackage v0.0.0-00010101000000-000000000000
)

When I try to run my mainFile with this sample content:

package mainfolder

import cust "username/GoLearn/custompackage"
import custfunc "username/GoLearn/examplepackage"

func main() {
	custfunc.examplefile2function() //this will be reported as not found 
	custfunc.examplefile1function() //this will be found and can be executed normally
    cust.customfilefunction() //this will be found and can be executed normally
}

I don’t exactly know why my newly created examplefile2 was not picked up by Golang, what I have tried so far is to use

go get .
go mod tidy

Can I get any help for this?

Hi @TriNguyen,

In general, functions must start with an uppercase letter in order to be found by other packages:

    func Examplefile2function()
    func Examplefile1function() 
    func Customfilefunction() 

Then, in mainfolder:

    custfunc.Examplefile2function() //this will be reported as not found 
    custfunc.Examplefile1function() //this will be found and can be executed normally
    cust.Customfilefunction() //this will be found and can be executed normally

BTW, the package that contains func main() must be named package main. Otherwise, the Go compiler cannot build an executable.

1 Like

it surely solved my problem, thank you so much Christopher.
Out of the box, is there any document about rules in Golang ? I’m new so maybe mistaking things a lot

1 Like

Go to the official website of the go.dev, golang is not very difficult, you can get started with it with a while, but the depth of things still needs to be specialized.

What @peakedshout said, and maybe check out Go by Example for an example-based approach.

For an interactive experience, take a look at Try Go in Y minutes.

This is a port from Learn Go in Y Minutes, but with code examples that you can run right from within the browser. (You may find differences between this and the original page. Blame me for that. I ported the original page over and took the liberty to restructure a few things and add generics.)