← Back to Go Course | Chapter 2: Variables & Types | Lesson 4 of 7

Basic Data Types

Go's basic types are the different kinds of boxes you can choose from: whole numbers, decimals, true/false, and text.

Integers

Go provides both a general-purpose int (whose size matches the platform, 32 or 64 bit) and explicitly sized integer types like int8, int16, int32, and int64, plus unsigned versions (uint8, uint32, etc.) for values that are never negative.

Note: Use plain int unless you have a specific reason (like memory layout or binary formats) to pick a sized variant.

Example: Integers

markup
package main

import "fmt"

func main() {
	var count int = 42
	var small int8 = 120
	var big int64 = 9000000000
	fmt.Println(count, small, big)
}

Floating-Point Numbers

float32 and float64 represent numbers with a decimal point. float64 is the default and generally preferred for precision unless memory is tightly constrained, since float32 loses accuracy for very large or very precise values.

Example: Floating-Point Numbers

markup
package main

import "fmt"

func main() {
	var price float64 = 19.99
	var discount float32 = 0.15
	fmt.Println("price:", price, "discount:", discount)
}

Booleans

The bool type holds exactly one of two values, true or false, and is the result of every comparison operator (==, <, >, etc.). Booleans control branching logic in if statements and loop conditions.

Example: Booleans

markup
package main

import "fmt"

func main() {
	isActive := true
	hasExpired := false
	fmt.Println("active:", isActive, "expired:", hasExpired)
}

Strings

A string in Go is an immutable sequence of bytes, most commonly holding UTF-8 encoded text. String literals are written in double quotes, and Go provides many built-in operators and standard-library functions for working with them.

Example: Strings

markup
package main

import "fmt"

func main() {
	greeting := "Namaste"
	fmt.Println(greeting, "length in bytes:", len(greeting))
}
Common Mistakes
  1. Using int everywhere and assuming it's always 64-bit -- its size actually depends on the target platform (32 or 64 bit).
  2. Mixing float32 and float64 values in the same expression without an explicit conversion, which Go's type system rejects.
  3. Comparing floating-point numbers with == for equality when rounding error can make two equal values differ slightly.
Chapter Summary
  • Go's core basic types include int, float64, bool, and string.
  • Integer types come in sized variants like int8, int16, int32, int64 and their unsigned counterparts.
  • float32 and float64 represent decimal numbers, with float64 the common default.
  • Go is strict: it never silently mixes types in an expression without an explicit conversion.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.