Go Variables, Constants, and Data Types
Estimated reading: 3 minutes 40 views

🧬 Go Data Types – Master Primitive & Composite Types in Go (2025 Guide)

🧲 Introduction – Why Data Types Matter in Go

Go is a statically typed language, which means every variable has a defined data type known at compile time. Understanding Go’s data types is essential to writing efficient, error-free, and type-safe code.

🎯 In this section, you’ll learn:

  • The basic (primitive) and advanced (composite) data types in Go
  • How Go categorizes types: numeric, string, boolean, etc.
  • When to use pointers, structs, arrays, slices, maps, and interfaces
  • Syntax examples and best practices

🔤 Primitive Data Types in Go

Go provides several built-in primitive types, divided into categories:

✅ Numeric Types

Type CategoryExamplesNotes
Integersint, int8, uint32Signed & unsigned, 8–64 bits
Floating-Pointfloat32, float64float64 is preferred
Complexcomplex64, complex128Used for complex numbers
Aliasesbyte (alias for uint8), rune (alias for int32)rune is for Unicode characters
var a int = 10
var b float64 = 3.14
var c rune = '✓'

✅ Boolean Type

var isActive bool = true
  • Can only be true or false
  • Used in control structures like if, for, etc.

✅ String Type

var name string = "GoLang"
  • Strings are immutable
  • Support UTF-8 by default
fmt.Println(len("Go"))       // Output: 2
fmt.Println("Go"[0])         // Output: 71 (ASCII of 'G')

📦 Composite Data Types in Go

Composite types are made up of one or more values or elements.

✅ Array

Fixed-size collection of elements of the same type.

var nums [3]int = [3]int{1, 2, 3}

✅ Slice

Dynamic array-like structure. More commonly used than arrays.

nums := []int{10, 20, 30}

✅ Map

Unordered key-value pairs.

student := map[string]int{"Alice": 90, "Bob": 85}

✅ Struct

Used to group related data into a custom type.

type User struct {
    Name string
    Age  int
}

✅ Pointer

Holds the memory address of a variable.

var x = 5
var p *int = &x
fmt.Println(*p)  // Output: 5

✅ Interface

Defines a set of method signatures. Core to Go’s polymorphism.

type Shape interface {
    Area() float64
}

🧪 Full Example – Using Multiple Types

package main

import "fmt"

type Book struct {
    Title  string
    Price  float64
}

func main() {
    var count int = 5
    price := 99.99
    tags := []string{"go", "programming", "backend"}
    book := Book{"Go Guide", price}
    scores := map[string]int{"Alice": 95, "Bob": 89}

    fmt.Println(count, price, tags)
    fmt.Println(book.Title, book.Price)
    fmt.Println(scores["Bob"])
}

📤 Output:

5 99.99 [go programming backend]
Go Guide 99.99
89

📌 Summary – Recap & Next Steps

Go provides a strong and expressive type system that includes primitive types for raw data and composite types for structured information. Mastery of these types allows you to write modular, maintainable, and efficient code.

🔍 Key Takeaways:

  • Use primitive types (int, float64, bool, string) for basic data
  • Use composite types (slices, structs, maps) to model complex structures
  • Pointers and interfaces enable advanced memory and design patterns
  • Go requires explicit typing unless using := for inference

⚙️ Next: Explore Go Type Conversions — converting between data types safely and efficiently.


❓ FAQs – Go Data Types

❓ What is the difference between int and int32 in Go?
int is platform-dependent (32-bit or 64-bit), while int32 is fixed-width. Use int for general purposes unless a specific size is required.

❓ When should I use a slice over an array in Go?
✅ Always use slices unless you need a fixed-size collection. Slices are more flexible and idiomatic in Go.

❓ Are strings in Go mutable?
✅ No. Strings are immutable. Use slices of bytes or runes for modifications.

❓ What is the zero value of a map or slice?
nil. You must initialize maps and slices using make() or literals before use.

❓ Can I mix data types in an array or slice?
✅ No. All elements in an array or slice must be of the same type. Use interface{} for mixed-type collections.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go – Data Types

Or Copy Link

CONTENTS
Scroll to Top