Go Data Types

Understanding different types of data in Go

🏷️ What are Go Data Types?

Go data types specify what kind of data a variable can store. Go has basic types like integers, floats, strings, and booleans, plus complex types like arrays and structs.


package main

import "fmt"

func main() {
    var age int = 25
    var name string = "Alice"
    var height float64 = 5.6
    var isStudent bool = true
    
    fmt.Println(age, name, height, isStudent)
}
                                    

Output:

25 Alice 5.6 true

Basic Go Data Types

🔢

Integer (int)

Whole numbers without decimals

var age int = 25
var count int = 100
🎯

Float (float64)

Numbers with decimal points

var price float64 = 19.99
var height float64 = 5.8
📝

String

Text and characters

var name string = "Alice"
var message string = "Hello!"

Boolean (bool)

True or false values

var isActive bool = true
var isComplete bool = false

🔹 Integer Types

Go has different integer types based on size:

package main

import "fmt"

func main() {
    var smallNum int8 = 127        // -128 to 127
    var mediumNum int16 = 32767    // -32,768 to 32,767
    var largeNum int32 = 2147483647 // -2,147,483,648 to 2,147,483,647
    var hugeNum int64 = 9223372036854775807
    
    var regularNum int = 42        // Platform dependent (32 or 64 bit)
    
    fmt.Println("int8:", smallNum)
    fmt.Println("int16:", mediumNum)
    fmt.Println("int32:", largeNum)
    fmt.Println("int64:", hugeNum)
    fmt.Println("int:", regularNum)
}

Output:

int8: 127
int16: 32767
int32: 2147483647
int64: 9223372036854775807
int: 42

🔹 Float Types

Go has two float types for decimal numbers:

package main

import "fmt"

func main() {
    var price32 float32 = 19.99    // 32-bit floating point
    var price64 float64 = 29.999   // 64-bit floating point (more precise)
    
    var pi float64 = 3.14159265359
    var temperature float32 = 98.6
    
    fmt.Printf("float32: %.2f\n", price32)
    fmt.Printf("float64: %.2f\n", price64)
    fmt.Printf("Pi: %.5f\n", pi)
    fmt.Printf("Temperature: %.1f°F\n", temperature)
}

Output:

float32: 19.99
float64: 29.99
Pi: 3.14159
Temperature: 98.6°F

🔹 String Operations

Working with strings in Go:

package main

import "fmt"

func main() {
    var firstName string = "John"
    var lastName string = "Doe"
    
    // String concatenation
    fullName := firstName + " " + lastName
    
    // Multi-line string
    message := `Hello,
    This is a multi-line
    string in Go!`
    
    // String length
    fmt.Println("Full name:", fullName)
    fmt.Println("Length:", len(fullName))
    fmt.Println("Message:")
    fmt.Println(message)
}

Output:

Full name: John Doe
Length: 8
Message:
Hello,
    This is a multi-line
    string in Go!

🔹 Boolean Operations

Using boolean values and logical operations:

package main

import "fmt"

func main() {
    var isLoggedIn bool = true
    var hasPermission bool = false
    var isAdmin bool = true
    
    // Logical operations
    canAccess := isLoggedIn && hasPermission
    canView := isLoggedIn || isAdmin
    isGuest := !isLoggedIn
    
    fmt.Println("Logged in:", isLoggedIn)
    fmt.Println("Has permission:", hasPermission)
    fmt.Println("Is admin:", isAdmin)
    fmt.Println("Can access:", canAccess)
    fmt.Println("Can view:", canView)
    fmt.Println("Is guest:", isGuest)
}

Output:

Logged in: true
Has permission: false
Is admin: true
Can access: false
Can view: true
Is guest: false

🔹 Type Inference

Go can automatically determine variable types:

package main

import "fmt"

func main() {
    // Short variable declaration (type inferred)
    name := "Alice"        // string
    age := 25             // int
    height := 5.6         // float64
    isStudent := true     // bool
    
    // Print types using %T
    fmt.Printf("name: %v (type: %T)\n", name, name)
    fmt.Printf("age: %v (type: %T)\n", age, age)
    fmt.Printf("height: %v (type: %T)\n", height, height)
    fmt.Printf("isStudent: %v (type: %T)\n", isStudent, isStudent)
}

Output:

name: Alice (type: string)
age: 25 (type: int)
height: 5.6 (type: float64)
isStudent: true (type: bool)

🧠 Test Your Knowledge

Which data type is used for decimal numbers in Go?