Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glued acronyms and golang naming convention

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
)
like image 731
ababo Avatar asked Oct 03 '16 11:10

ababo


People also ask

Is Golang a camelCase?

In Golang, any variable (or a function) with an identifier starting with an upper-case letter (example, CamelCase) is made public (accessible) to all other packages in your program, whereas those starting with a lower-case letter (example, camelCase) is not accessible to any package except the one it is being declared ...

Does go use camel case or snake case?

The convention in Go is to use MixedCaps or mixedCaps (simply camelCase) rather than underscores to write multi-word names. If an identifier needs to be visible outside the package, its first character should be uppercase. If you don't have the intention to use it in another package, you can safely stick to mixedCaps .


1 Answers

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".

like image 149
icza Avatar answered Oct 14 '22 04:10

icza