I am having some trouble understanding that why my code complains when I have hyphen in package. e.g. if I have a package name foo-bar and I declare that package name
package foo-bar
foo-bar/config.go:1:13: expected ';', found '-'
Then why does the Go compiler complain? Does it mean we should not use hyphens in go package names?
Since there are many repo that has use hyphen in package name, am I doing something wrong?
We can see from the Go spec that a package name must be a valid identifier:
PackageName = identifier .
We can further read that a valid identifier is defined as:
identifier = letter { letter | unicode_digit } .
So package names may contain only letters and digits. -
characters are not permitted.
We can further read that as a bit of a special case, the underscore character (_
) is defined as a letter, for the purpose of Go identifiers:
The underscore character _ (U+005F) is considered a letter.
So you may substitute -
with _
for your package name if you wish.
However, please consider not doing so, as it's considered non-idiomatic. For advice on package naming in Go, please read the section of Effective Go on package names, or read The Go Blog's post on Package Names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With