Golang variables and data types for beginners
Let’s get Going (Sorry, Not sorry)
It’s been too long!
I know it’s been a while since I last posted, and there’s been a couple of reasons why. And if you want to skip my explanation, you can scroll past to the Key terms section.
Firstly, an HTTP server was perhaps a little difficult to build for a beginner, and I was far too enthusiastic to believe that I could do it so early on. I needed to be realistic about my skill level and build myself up with more of the fundamentals of coding before that happened.
Secondly, I’ve been trying to find the right resources to help me learn these fundamentals with examples and guides on how to use them. Again, a lot of resources that I’ve found are great for people who are already capable developers, understand what many of the terms mean and how they’re applied. I didn’t, and I won’t claim that I understand them all now, I’ve got cheat sheets galore to help me out until it becomes more like muscle memory.
To build a good foundation I bought a course from Udemy.com, which wasn’t too expensive and I learnt a great deal. One thing that I would say is that if you choose to take this course, stick with it. It may be slow to start but the practical examples that they provide to teach you the basics are really worth while. Also, they don’t use Visual Studio, so if you do choose to do the course make reference to either my earlier blog or any tutorial pages that explain how to use Visual Studio alongside it.
Don’t forget that I always include a list of all my resources at the end of the blog!
Key terms and how to implement them
The first half of this blog is going to look at key terms and their implementation in Go. The second half is going to show how I’ve used them in an example.
Variables and Constants
Variables are used to store data that can be referenced or altered in a programme and are given easy names to help us understand what they are and do.
Variable (or ‘var’) — can change depending on the conditions or the information that is passed to it by the programme.
Constant (or ‘const’) — a variable that can’t be changed.
Think of it this way, my name is constant, my age is variable (unfortunately, no one can be 21 forever).
Data types
To be honest, learning to code is very much about learning to speak a new form of English, not just in how you write the code, but the terms that are used. What I would recommend is always keep cheat sheets, like I said earlier, but also keep hold of a list of terms like the ones below for reference and I’ll look at how they are used in practice in some examples later.
Boolean — It’s a true/false variable. There are three operators that you can use with Bools (‘and’, ‘or’ and ‘not’), which we will explore in more detail on at a later date.
Integer — These are whole numbers. They can be positive, negative or equal zero but they can’t contain a decimal place. Those that contain a decimal are called Floats.
Unsigned Integers — They are whole numbers and are always positive (0+). For example, when playing a game there will never be a negative number of players, there will be either 0 or more. These are quite advanced and not used except for seriously large numbers like the number of stars of the universe. But, most people just use integers however, I thought it was worth knowing that it existed.
String — A value contains characters and numbers contained in quotation marks for example, “I’m learning how to code in 2018”.
Functions
You’ll remember from the last blog when we built the ‘Hello, World’ programme I introduced func main(). Functions are also a data type and can be assigned to variables, but this is a bit more advanced, and something that I’ll look into at a later date.
The func main() is the entry point to all Go programmes, however as your programs grow you will use other functions.
Under one package there can be a number of functions. Functions are marked at the start and end by {} which tells the programme where it starts and ends.
Let’s see this all in action!
As ever, all the code is annotated with comments marked with //.
**I’ve been told that where I’ve added the comments here are incorrect in developer terms and are usually added above. For the sake of education and flow, I’ve put them below so you can see what I’ve done and why. If I was actually annotating code for production I would put the comments above.
I introduced some principals here that I hadn’t discussed before.
You’ll notice that on line 5 that “reflect” was imported as well as the ‘format’ (fmt) package. This is because when I wrote an fmt.Println on line 23 the ‘reflect.TypeOf’ function printed out the variable type that was used to help me understand what my code was doing, I can see that I have a ‘string’ (Name), ‘int’ (age) and a ‘float64’ (heightInMeters).
The heightInMeters declaration is odd too. A majority of languages including Go are case sensitive so that it seems odd to introduce capitals in the middle of something like heightInMeters. This happens in Go because it’s just the preferred syntax and makes it easier to read, effectively all the design decisions for Go have been made to optimise readability.
Review
I hope that in this blog I’ve been able to cover some basic principles and some not so basic ones in practice. The code that I’ve copied into the blog above all works, so why not try it yourself? You can reference back to the previous blog to show you get the output in the terminal.
The issue that I had with this blog was going from not enough content to having too much! The next post is going to approach Operators and ‘If’ Statements and show them in action.
As ever, thank you very much for reading and supporting the blog. If you have any feedback or questions, feel free to tweet me :)
Bibliography