简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM