Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package name containing hyphen

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?

like image 847
Aman Chourasiya Avatar asked Aug 17 '20 07:08

Aman Chourasiya


Video Answer


1 Answers

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.

like image 198
Flimzy Avatar answered Sep 20 '22 03:09

Flimzy