Types Fundamentals

Loading concept...

🎨 The Magic Box of Types in Go

Imagine you have a magic toy box with special compartments. Each compartment can only hold one specific type of toy. You can’t put a teddy bear in the car compartment! That’s exactly how types work in Go.


🌟 What Are Types?

Think of types like labels on containers.

  • A jar labeled “Cookies” holds cookies
  • A jar labeled “Marbles” holds marbles
  • You don’t mix them up!

In Go, every piece of data has a type. The type tells Go:

  1. What kind of data it is
  2. What you can do with it
  3. How much space it needs

✅ Boolean Type: Yes or No, That’s It!

The Simplest Question

A boolean is like a light switch. It’s either ON or OFF. Nothing in between!

var isHappy bool = true
var isSad bool = false

Real Life Examples:

  • Is the door open? true or false
  • Did you eat breakfast? true or false
  • Is it raining? true or false

Boolean Magic

Booleans help your program make decisions:

var hasMoney bool = true
var wantIceCream bool = true

// Both must be true to buy!
if hasMoney && wantIceCream {
    fmt.Println("Buy ice cream!")
}

Key Point: Boolean values are named bool in Go. They only hold true or false.


🔢 Integer Types: Counting Numbers

Whole Numbers Only!

Integers are like counting on your fingers. No fractions, no decimals. Just whole numbers!

var apples int = 5
var cookies int = 12
var temperature int = -10  // negatives work too!

Different Sizes of Boxes

Go has different “box sizes” for integers:

Type Range Use When
int8 -128 to 127 Tiny numbers
int16 -32,768 to 32,767 Small numbers
int32 ~-2 billion to ~2 billion Regular numbers
int64 Huge range! Really big numbers
int Depends on computer Most common choice

Unsigned integers (only positive):

var age uint8 = 25      // 0 to 255
var stars uint = 1000   // no negatives allowed!

Why Different Sizes?

Think of it like choosing a backpack:

  • Small backpack for a short trip (int8)
  • Big suitcase for vacation (int64)
  • Why carry a huge suitcase to school?
var smallNumber int8 = 100   // fits perfectly!
var bigNumber int64 = 9999999999  // needs space

🎈 Floating-Point Types: Numbers with Dots

When You Need Pieces

What if you have 2.5 pizzas? That’s a floating-point number!

var pizza float64 = 2.5
var temperature float64 = 98.6
var price float32 = 19.99

Two Flavors

Type Precision Use When
float32 ~7 digits Games, graphics
float64 ~15 digits Math, money, science

Example:

var pi float64 = 3.14159265359
var shortPi float32 = 3.14159

Watch Out! Floating Math Can Surprise You

var result float64 = 0.1 + 0.2
// result is 0.30000000000000004
// not exactly 0.3!

This happens because computers store decimals in a special way. It’s like trying to write 1/3 as a decimal (0.333…) - it never ends perfectly!


🔄 Type Conversion: Changing Costumes

Numbers Can Change Outfits!

Sometimes you have an integer but need a float. Or vice versa. Go lets you convert between types.

var myInt int = 42
var myFloat float64 = float64(myInt)
// myFloat is now 42.0

The Golden Rule

You must explicitly convert. Go won’t do it secretly!

var age int = 25
var price float64 = 19.99

// This WON'T work:
// total := age + price  // ERROR!

// This WILL work:
total := float64(age) + price  // 44.99

Converting Between Integer Types

var big int64 = 1000
var small int8 = int8(big)  // Be careful!
// small is now -24 (overflow!)

Warning: If the number is too big for the new type, weird things happen! It’s like pouring a gallon into a cup - it overflows!

Float to Int Drops Decimals

var price float64 = 9.99
var rounded int = int(price)
// rounded is 9 (not 10!)

The decimal part just disappears. No rounding!


🔮 Type Inference: Go Reads Your Mind!

Let Go Figure It Out

You don’t always need to write the type. Go is smart enough to guess!

// The long way:
var name string = "Gopher"

// The short way (type inference):
name := "Gopher"  // Go knows it's a string!

How Does Go Know?

Go looks at the value and picks the type:

x := 42        // int (whole number)
y := 3.14      // float64 (has decimal)
z := true      // bool (true/false)
s := "hello"   // string (text)

The := Magic Shortcut

The := operator does two things at once:

  1. Creates the variable
  2. Figures out the type
// These two lines do the same thing:
var count int = 10
count := 10

When Type Inference Picks Float64

pi := 3.14  // Always float64, never float32

Go defaults to float64 for decimals because it’s more precise.

Mixing It Up

age := 25           // int
height := 5.9       // float64
isStudent := true   // bool

// Go figured out all three types!

🎯 Quick Summary

graph TD A[Go Types] --> B[bool] A --> C[integers] A --> D[floats] B --> B1[true / false] C --> C1[int, int8, int16...] C --> C2[uint, uint8, uint16...] D --> D1[float32] D --> D2[float64]
Concept What It Means Example
bool Yes/No values true, false
int Whole numbers 42, -7
float64 Decimal numbers 3.14, 9.99
Conversion Change type explicitly float64(myInt)
Inference Go guesses type x := 42

🚀 You’re Ready!

Now you know that:

  1. Booleans are simple on/off switches
  2. Integers count whole things
  3. Floats handle pieces and decimals
  4. Conversion changes one type to another (explicitly!)
  5. Inference lets Go figure out types for you

Think of types as labels that keep your data organized. When you respect the labels, your programs run smoothly!


Next time you write Go code, remember: every value has a home, and that home is its type! 🏠

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.