Is there a way to make the constants below to be more readable without breaking golang naming convention?
const ( // stream types
MPEGDASHStream = iota
HLSStream = iota
MPEGTSUDPStream = iota
MPEGTSRTPStream = iota
)
Is there a way to make the constants below to be more readable without breaking golang naming convention?
const ( // stream types
MPEGDASHStream = iota
HLSStream = iota
MPEGTSUDPStream = iota
MPEGTSRTPStream = iota
)
Go's naming convention prefers MixedCaps rather than underscores, so don't use them. Source: Effective Go: MixedCaps
Usually when you have constants for different values of an entity, a more easily readable way is to start constant names with the entity, which is then followed by the name of the concrete value. Great examples are the net/http
package:
const (
MethodGet = "GET"
MethodHead = "HEAD"
MethodPost = "POST"
// ...
)
const (
StatusContinue = 100 // RFC 7231, 6.2.1
StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
StatusProcessing = 102 // RFC 2518, 10.1
StatusOK = 200 // RFC 7231, 6.3.1
StatusCreated = 201 // RFC 7231, 6.3.2
// ...
)
Also you don't need to repeat the expression with iota
identifier. Spec: Constant declarations:
Within a parenthesized
const
declaration list the expression list may be omitted from any but the first declaration. Such an empty list is equivalent to the textual substitution of the first preceding non-empty expression list and its type if any. Omitting the list of expressions is therefore equivalent to repeating the previous list.
So in your case it could simply look like this, which is quite clear and readable:
// stream types
const (
StreamMPEGDASH = iota
StreamHLS
StreamMPEGTSUDP
StreamMPEGTSRTP
)
Also see Go Code Review Comments for more details. Acronyms can be found in the Initialisms section:
Words in names that are initialisms or acronyms (e.g. "URL" or "NATO") have a consistent case. For example, "URL" should appear as "URL" or "url" (as in "urlPony", or "URLPony"), never as "Url". Here's an example: ServeHTTP not ServeHttp.
This rule also applies to "ID" when it is short for "identifier," so write "appID" instead of "appId".
What you do is the canonical Golang way.
According to Code Review Comments, you should string the acronyms in ALLCAPS
:
Words in names that are initialisms or acronyms (e.g. "URL" or "NATO") have a consistent case. For example, "URL" should appear as "URL" or "url" (as in "urlPony", or "URLPony"), never as "Url". As an example: ServeHTTP not ServeHttp. For identifiers with multiple initialized "words", use for example "xmlHTTPRequest" or "XMLHTTPRequest".
Do I like it? Not so much.
You could move the words around a bit to make it more readable, e.g. MPEGStreamDASH
If you really hate it - then you need to realize it's your code and you can do whatever you like - be it MpegDashStream
, MPEG_DASH_Stream
or even MpEg_DaSh_StreaM
- but that would be evil ;-)
My understanding from https://google.github.io/styleguide/go/decisions.html (specifically each initialism in the name does not need to have the same case
) is that MPEGdashStream
would be correct.