Assigning a variable
//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.
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
sport string = "Football"
position string = "Quarterback"
playerName string = "Jimmy Garoppolo"
playerNumber int = 10
Go also has constant variables. These variables are immutable meaning that once they are assigned, cannot be redeclared with a new value.
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 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.
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!