Thuta Learning
BasicProgrammingbeginner

Data Types

Relax. We'll talk through this in plain words — no textbook voice.

Go is a statically typed language, so the compiler needs to know what type a variable will hold. Once you understand the basic types, writing functions, structs, maps, and slices becomes much easier.

go
package main

import "fmt"

func main() {
    age := 25
    price := 19.99
    name := "ThutaTech"
    active := true

    fmt.Printf("age: %T\n", age)
    fmt.Printf("price: %T\n", price)
    fmt.Printf("name: %T\n", name)
    fmt.Printf("active: %T\n", active)
}

%T is the format verb that prints out a value's type. Integer, float, string, and boolean are the basic building blocks of Go.

You should see
age: int price: float64 name: string active: bool

Info

Money calculations can run into floating point precision issues, so in production you should consider a decimal package or representing amounts as integer cents.

Easy traps

  • You can't add a string and a number directly. You'll typically need the strconv package to convert between them.
Data Types | Thuta Learning