Start learning free

LESSON 10 OF 338 · VARIABLES

Data Types in Go: int, float64, string and bool

Lesson Read the full explanation

Every value in Go has a type. The type tells Go what kind of data the value represents.

Common Go types include:

  • int for whole numbers, like 5
  • float64 for decimal numbers, like 3.14
  • string for text, like "Hello"
  • bool for truth values, true or false
var age int = 31
var price float64 = 9.99
var name string = "Andy"
var isStudent bool = true

Assignment
Follow the Coding Tutorial and create variables of different Go types.


Hint
Write the type after the variable name when using var.


STEP 1 OF 4
    Ctrl + Enter to check

    GO · VARIABLES

    Data Types in Go, explained

    What a type is

    Every value in Go has a type. The type tells Go what kind of data the value is, which operations are allowed on it, and how much memory it needs. Go also fixes the type of a variable when you declare it: a variable that starts as a whole number can never hold text later.

    That strictness lets the compiler catch many mistakes before the program runs. It also means you need to know the basic types and pick the right one for each value.

    The four basic types

    TypeHoldsExample values
    intwhole numbers5, -12, 0
    float64decimal numbers3.14, -0.5
    stringtext"Hello", ""
    booltruth valuestrue, false

    Go has more numeric types, such as int64, uint8 and float32, but these four cover most beginner code.

    Declaring a variable with a type

    With var, the type goes after the name, and then comes the value:

    var age int = 31
    var price float64 = 9.99
    var name string = "Andy"
    var isStudent bool = true
    

    Read each line as "variable name of type T starts as value". This order, name first and type second, is the reverse of many other languages.

    Checking a type

    The %T verb of fmt.Printf prints the type of a value:

    var myInt int = 5
    var myFloat float64 = 3.14
    var myString string = "Hello"
    var myBool bool = true
    fmt.Printf("%T %T %T %T\n", myInt, myFloat, myString, myBool)
    
    int float64 string bool
    

    Letting Go infer the type

    You will often see the shorter form :=, which works out the type from the value. A whole number like 5 becomes an int, a number with a decimal point like 3.14 becomes a float64, quoted text becomes a string, and true or false becomes a bool. The type is just as fixed as before; you simply didn't write it. This is why x := 7 is an int but x := 7.0 is a float64, even though the numbers are equal. The lesson writes the types out so that you can see them.

    Choosing a type

    Pick the type that matches the meaning of the data. Counts, ages and positions in a list are int. Measurements and prices with decimals are float64. Names and messages are string. Yes/no facts, such as whether a user is a student, are bool, and it helps to name them like questions, as in isStudent.

    Gotchas

    • Integers and floats are different types. A variable n holding an int and one called myFloat holding a float64 can't be multiplied: Go reports mismatched types int and float64. Convert one of them, for example float64(n) * myFloat.
    • A decimal doesn't fit into an int. var myInt int = 3.5 is rejected, because 3.5 isn't a whole number. On the other hand, var f float64 = 7 is fine, and f prints as 7.
    • Quotes matter. "true" is a string, so it can't initialise a bool. A bool value is written without quotes.
    • Division depends on the type. With int values, 7 / 2 is 3, because the fractional part is dropped. With float64 values, 7.0 / 2.0 is 3.5.

    Zero values

    A variable declared without an initial value isn't left undefined. Go gives it the zero value of its type: 0 for int and float64, "" (the empty string) for string, and false for bool. So var count int starts at 0.

    The exercise, step by step

    The lesson builds four variables, one per line, each with a different type.

    1. An integer. Declare myInt with type int and give it a whole number such as 5.
    2. A float. On the next line, declare myFloat with type float64 and give it a decimal such as 3.14. Keep myInt.
    3. A string. Declare myString with type string and give it text in double quotes, for example "Hello".
    4. A bool. Declare myBool with type bool and set it to true or false.
    var myInt int = 5
    var myFloat float64 = 3.14
    var myString string = "Hello"
    var myBool bool = true
    

    Each variable uses the same pattern: var, name, type, =, value.

    Key takeaways

    • Every value has a type, and a variable's type never changes.
    • The four basic types are int, float64, string and bool.
    • In a var declaration, the type comes after the name.
    • Go doesn't mix int and float64 in one expression without a conversion.
    • An uninitialised variable gets its type's zero value.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free