Video Coming Soon...

Created by Zed A. Shaw Updated 2025-10-07 14:32:54

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:

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.

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:

  1. 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.
  2. Change It -- Make as many changes as you can to this code to experiment and explore what it means.
  3. 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.

  1. Give every variable a value and try different values.
  2. Try to use every type of variable and print them.
  3. Try to do various types of math calculations with each type.
  4. Try to use different types together. Can you add a number to string? Can you add a float to an int?
Previous Lesson Next Lesson

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.