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.
Notice that you can't initialize an instance and assign it to a const.
Instead, you must assign it to a var like so:
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."
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:
Your comment will be posted after it is approved.
Leave a Reply.
The posts on this site are written and maintained by Jim Lynch. About Jim...