Home > Net >  Does GoLang use reserved filenames?
Does GoLang use reserved filenames?

Time:12-10

In a CI/CD pipeline, we have a Go script that does some preprocessing prior to actually building our deliverable artifacts. If I name the script main.go is that a foul in the GoLang world or is that OK? I do not understand if there are any namespace concerns that tie back to the actual filename of the script.

I know that main.go being the script, main_test.go is the Unit Tests so I am leary and looking for guidance.

For example, if I execute from the shell (MacOS bash) the following: go test This executes the unit tests for the main.go script which leads me to believe that main is some sort of reserved file name. I could easily be wrong though.

Thoughts?

CodePudding user response:

The filename main.go is not special.

Go detects that a file is part of a test because it ends in _test.go, and detects that a package is a binary (instead of a library) because the package name is main (in other words, it starts with package main). You can use a file named main.go in a library.

You can see in the source code for go/build what go build looks for in files. It only has a check for the _test suffix, here:

https://cs.opensource.google/go/go/ /refs/tags/go1.17.3:src/go/build/build.go;l=895;drc=deb3403ff52b8833df6c4e2f82cbdddeb13573dd

isTest := strings.HasSuffix(name, "_test.go")
  •  Tags:  
  • go
  • Related