Variables in the Go Programming Language(golang)

Before going into how Go goes about handling its variables I want to do a brief explanation of the language and why someone would want to use the language in the first place. The Go programming language(also referred to as golang)is an open-source language that was developed at Google. The problem that Go tries to solve is that other languages at the time like C, Python, and Java had really slow compile times, and had complex type systems. Go is statically typed, has very fast compilation times, easy to read, and has garbage collection. Go is very unique with how it goes about its syntax. It’s it is taking elements from other programming languages like JavaScript, C, Python, and puts it together into an easy to read format.
Assigning a variable
If you’ve written in the JavaScript before this syntax will look very familiar. Go uses the “var” keyword followed by the name of the variable and the datatype associated with that variable. As is we are declaring the variable but has no value, we are telling go we are going to assign it a value at a later time.
//Variable is declared.
var codingLanguage string//Variable is assigned.
codingLanguage = "GO"
This is fine but as good developers we want to have our code to look nice and clean, so we can actually just write all this in one line
var codingLanguage string = "golang"
There is also another method to assigning a variable that is even cleaner than the last one.
number := 11
This method of assigning a variable is using type inference. Go will assume it’s datatype based on the value it is assigned to it. There is one caveat to this, the above variable is recognized as an integer, what if we don’t want it to be an integer, but a float? Just simply add a “.” at the end and it will be recognized as a float64 datatype(note: you cannot set it as a float32 with this method so keep that in mind).
*** Note: The “:=” operator can only be used within a function, you cannot assign variables this way within the global scope.
Block variables
In Go you can also write a variable block, lets say I want to take down attributes for athletes. Instead of writing it like this:
var sport string = "Football"
var position string = "Quarterback"
var playerName string = "Jimmy Garoppolo"
var playerNumber int = 10
we can make it a bit easier to read this way and have the same functionality
var (
sport string = "Football"
position string = "Quarterback"
playerName string = "Jimmy Garoppolo"
playerNumber int = 10
)
Constants
Go also has constant variables. These variables are immutable meaning that once they are assigned, cannot be redeclared with a new value.
Ex:
const randomFact string = "Pteronophobia is the fear of feathers or being tickled by feathers"randomFact = "Bananas are curved because they grow closer to the sun"//This would return an error because we've already declared that the random fact will be and it cannot be overwritten
with constants you may also exclude the datatype if you so wish and Go will infer the datatype based on the value assigned, just like the “:=” operator.
Shadowing
Shadowing pertains to scope. Shadowing is when you have a variable with in the global scope, as well as that same variable within that function’s scope. The variable within the inner-most scope will take precidence.
As you can see by the output the first time we print favoriteMeme we get it on the global scope level, then once it’s reassigned in the function’s scope, favoriteMeme is now Doge.
Converting Datatypes
You may also change the datatypes of previously assigned variables in Go.
In the example below, first we have a variable “a” assigned to an int with the value 11. We have variable “b” declared as a float32, then we are assigning b’s value to equal a but wrapped in a float32 function in order to convert a’s value to float. If we applied this same logic to converting an integer to a string, we actually don’t get the output we would expect. If we tried to convert an int to a string with the same method, it would return us the unicode character that is tied to that integer value. So in that case we would need a separate package called strconv to convert our integer to a string of the same numerical value.
I love the simplicity and readability of variables in the Go programming language and hope this clears some things up regarding on how to work with variables in Go!
As always, Happy Coding!