Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Are hyphens and/or underscores valid in golang package names?
Can a Go package name validly contain a hyphen and/or an underscore?
The godocs say that
By convention, packages are given lower case, single-word names; there should be no need for underscores or mixedCaps.
However, this does not indicate if underscores are invalid/illegal vs. simply not following the convention, and it says nothing about hyphens.
Similarly, I searched for "hypen" and "underscore" in a blog post on Go package names and found nothing about hyphens. There was an example with an underscore in a package name with an introductory comment indicating that it does "not fit well in Go," but again does not indicate whether or not it is invalid.
1 answer
Disclaimer: I don't know Go. This is all just me reading the specification.
In the Go specification under Packages, it defines PackageName
.
PackageName = identifier
Under Identifiers
identifier = letter { letter | unicode_digit } .
Under Letters and digits
letter = unicode_letter | "_" .
Underscores are therefore legal, according to the spec.
Under Characters, it says what constitutes a unicode_letter
and unicode_digit
.
unicode_letter = /* a Unicode code point classified as "Letter" */ . unicode_digit = /* a Unicode code point classified as "Number, decimal digit" */ .
In The Unicode Standard 8.0, Section 4.5 "General Category" defines a set of character categories. Go treats all characters in any of the Letter categories Lu, Ll, Lt, Lm, or Lo as Unicode letters, and those in the Number category Nd as Unicode digits.
0 comment threads