Go Has An Extremely Strict Definition of “Constants”

Jim Lynch Codes
3 min readDec 6, 2019

--

This is something that often trips up newcomers to Go who are familiar with JavaScript. I think it’s pretty fascinating to notice whether different programming languages decide to do things in different ways, and it’s interesting that the concept of “constants” is so different in Go Lang and JavaScript. has an extremely strict definition

In Go, Constants Can Only Be A Few Primitive Types

Suppose you have a simple type struct in Go that defines you Person data as having a public (designated by the capital initial letter) FirstName string property and a public LastName string property.

type Person struct {
FirstName string
LastName string
}

Notice that you can't initialize an instance and assign it to a const.

// Note- In Go you can't instatiate Types to a "const" variable.
// Both of these result in compile errors:
const p4 = new(Person)
const p5 := new(Person)

Instead, you must assign it to a var like so:

// long form syntax 
var p2 = new(Person)
p2.FirstName = "Cindy"
p2.LastName = "Jacobs"

// shortcut syntax
p3 := Person{"Matt", "O'Reiley"}

​In Go, constants can only be fixed, literal values. The tour of go constants page puts it nicely, "Constants can be character, string, boolean, or numeric values." although I would phrase it more like, "Constants can ONLY be a character, string, boolean, or numeric value."

In JavaScript, The Only Rule With Constants Is They Can't Be Re-Assigned

In comparison to Go, JavaScript seems like the wild west where anything is possible. In JavaScript, the only rule for constants is that constants cannot be reassigned. However, JavaScript constants can be collections, and you are free to mutation and reassign the things inside of the collections, just not the const holding the entire collection. For, consider this code Node.js code in TypeScript:

interface IPerson {
firstName: string;
lastName: string;
}

class Person implements IPerson {
constructor(public firstName: string = "First", public lastName: string = "Last") { }
}

const p : IPerson = new Person();

p.firstName = "123"
p.firstName = "wombat"
p.firstName = "DinkleWomp"

console.log(p.firstName) // prints "DinkleWomp"

const someArray = [1, 2, 3]

someArray[0] = 42
someArray.push(4)

console.log(someArray) // prints [ 42, 2, 3, 4 ]

Notice how you can mutate the properties of objects and even push new items into arrays that are so-called "constants". hehe. Also, notice how much more verbose the TypeScript is; we need to create this interface structure, then make the class blueprint containing information about both functions and data, and finally instantiate a usually object. In Go, we just define a "type struct" to define the shape our of data, an instantiatable interface of properties if you will. We can create methods that take arguments of a given struct type as a receiver. Personally, I like to think of it al being almost like adding instance methods to the struct type. Here's an example:

type Person struct {
Name string
}
func (p Person) Talk() {
fmt.Println("Hi, my name is", p.Name)
}

func main() {
person := Person{"Jimbo"}
person.Talk() // prints "Hi, my name is Jimbo"
}

Which Do You Prefer?

I'm curious to hear whether people like Go Lang's super strict definition of constants or if you prefer the now idiomatic JavaScript "const-all-the-things" style of coding? I'm torn as I can appreciate both and see the pros and cons of each, but I’d say I guess slightly prefer the Go Lang approach since it is less code overall and seems to be a nice hybrid of useful concepts of "classes" and "interfaces" from languages such as TypeScript. Please do let me know what you think in the comments below! Happy coding!

--

--