Package name containing hyphen

10,336

Solution 1

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.

Solution 2

from Go Package Name blog post:

The style of names typical of another language might not be idiomatic in a Go program. Here are two examples of names that might be good style in other languages but do not fit well in Go:

  • computeServiceClient
  • priority_queue

If your package address has - in name, your can set your package name with an underline.

Example:

package file address : some/where/foo-bar/config.go

package foo_bar

// your codes
Share:
10,336
Aman Chourasiya
Author by

Aman Chourasiya

Updated on July 26, 2022

Comments

  • Aman Chourasiya
    Aman Chourasiya almost 2 years

    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?