Constants can be confusing and easy to misuse in Go if you are coming from an untyped language. In this article we will learn what constants are, and when it is best to make use of them.

Most of the points I make here apply to general-purpose programming languages as a whole, but by focusing specifically on Go we can dive a bit deeper and come up with some good rules of thumb.

Go vs Javascript

Go > Javascript

Many languages have constants, typically denoted by the keyword const.

In go and javascript alike they are declared as such:

const frameRate = 60

The interesting thing about constants in Go is that they:

  • Must be able to be assigned at compile time. The value of a const can’t be the result of a runtime calculation.
  • Run faster because the compiler can make specific optimizations.
  • Cannot change. The compiler will not allow them to be re-assigned.
  • Only work with some types. Arrays, Slices, Maps, Structs, etc… can’t be made constant
  • Are not normal Go types unless explicitly assigned as such

#cryptography #compile #constant #global #go #golang #javascript #const

Constants in Go Vs. Constants in Javascript
1.15 GEEK