styfle
styfle

Reputation: 24610

Does Go have file name restrictions?

I created a file with square brackets called [id].go but I am unable to build it.

When I run go build "[id].go", I see the the following:

can't load package: package main: invalid input file name "[id].go"

Are there restrictions on Go file names? Specifically, what is not allowed? Please provide documentation if any.

Upvotes: 4

Views: 2572

Answers (1)

mkopriva
mkopriva

Reputation: 38233

At the time of writing, Go files must begin with one of the following:

  • 0 through 9
  • a through z
  • A through Z
  • . (period)
  • _ (underscore)
  • / (forward slash)
  • >= utf8.RuneSelf (char 0x80 or higher)
  • Two or more files in the same folder can't be named equal (case insensitive match)

https://github.com/golang/go/blob/993ec7f6cdaeb38b88091f42d6369d408dcb894b/src/cmd/go/internal/load/pkg.go#L1826-L1835

To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII.

As an example if you try a[id].go as the file name you should be good to go.

Upvotes: 4

Related Questions