Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Go Has An Extremely Strict Definition of "Constants"

12/5/2019

0 Comments

 
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 
var p6 := 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 JavaScript now idiomatic "const-all-the-things" style of coding? I'm torn as I can appreciate both and see the pros and cons of each, I 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! ?
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023