Video Coming Soon...
07: Basic Syntax: Types
WARNING This exercise is in DRAFT status, so there may be errors. If you find any, please email me at help@learncodethehardway.com so I can fix them.
When you create a variable in any programming language you are reserving some space in your computer's memory for data. The size of this piece of memory can range from 1 byte to 16 bytes in size depending on the type of variable. You can also have a variable type called a "string" which is a list of characters in some text you want to print.
Integer numbers (byte
, int
, int
) have the concept of "signed" and "unsigned" as well. A signed variable means that it can represent negative and positive values. An unsigned variable means it can only represent positive values, but it can represent a larger range of values.
The official list of base types available to you are:
bool
- This representstrue
orfalse
values.byte
- A raw byte of memory. It's an alias foruint8
int
int8
int16
int32
int64
-- Integers of different sizes from 8 bits to 64 bits, signed.uint
uint8
uint16
uint32
uint64
uintptr
-- The same as theint
numbers, but unsigned so no negative numbers but can hold larger values.float32
float64
-- Floating point numbers, or you may call them decimal or real numbers.complex64
complex128
-- Complex numbers. I don't know, it's a math thing you will probably never use.rune
- Holds unicode characters.string
- Textual strings that you typically store or print.
The Code
I now give you more code to enter and study. Remember to type this in gradually. If you find yourself blasting all of it into a file and compiling once then start over.
View Source file ex07/main.go Only
package main
import (
"fmt"
)
func main() {
var age int
var name string
var height float64
var mind complex64
fmt.Println("age:", age)
fmt.Println("name:", name)
fmt.Println("height:", height)
fmt.Println("mind:", mind)
}
You should also notice that I use a slightly different import
:
import (
"fmt"
)
We'll use this later in another exercise when we need more than one package.
The other thing to notice in this code is that I haven't set these variables to anything. In many languages this would be an error, but in Go the variables are given default "zero values."
The Breakdown
I'm not going to break down most of the lines you already know, just the new ones.
var age int
-- Create anint
variable namedage
.var name string
-- Create astring
variable namedname
.var height float64
-- Create afloat64
variable namedheight
.var mind complex64
-- Create acomplex64
variable namedmind
...because I can't think of anything you'd want to use a complex number for.fmt.Println("age:", age)
-- Print outage
.fmt.Println("name:", name)
-- Print outname
.fmt.Println("height:", height)
-- Print outheight
.fmt.Println("mind:", mind)
-- Print outmind
.
The Practice
I covered the "practice" in Exercise 6 that you should follow to improve your skills in programming, but let's cover it again here:
- Break It -- You need to learn what error messages mean and what causes them, so break this code on purpose in as many ways as possible.
- Change It -- Make as many changes as you can to this code to experiment and explore what it means.
- Remake It -- Write up a description of the code, hide or delete this, then try to recreate it using the process from Exercise 04: How to Write Code. Try to do this from memory as much as possible.
As I've mentioned before, the more you do these three things the more solid your understanding of programming becomes.
Study Drills
At the end of many exercises I'll give you open ended challenges to complete. If you can't figure one out, then write it down and come back later. Many times you'll learn additional information in a later exercise that helps you figure out these here.
- Give every variable a value and try different values.
- Try to use every type of variable and print them.
- Try to do various types of math calculations with each type.
- Try to use different types together. Can you add a number to
string
? Can you add afloat
to anint
?
Register for Learn Go the Hard Way
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.