Go Basics

Go Data Types

Description

Go is a statically typed language. Basic types include `bool`, `string`, and numeric types like `int`, `float64`, `complex128`. Go variables are initialized to their zero value if not assigned (0, false, "").

Pros & Cons

✅ Pros

  • Precision: Explicit bit-sizes (int8, int64).
  • Safety: No implicit casting.

❌ Cons

  • Verbosity: Need explicit conversions (e.g. int to float).

When to Use

Defining the structure of your data.

Example

Viewing basic types and zero values.

var (
    ToBe   bool       = false
    MaxInt uint64     = 1<<64 - 1
    z      complex128 = cmplx.Sqrt(-5 + 12i)
)

func main() {
    var i int
    var f float64
    var b bool
    var s string
    // Prints zero values: 0 0 false ""
    fmt.Printf("%v %v %v %q\n", i, f, b, s)
}

Best Practices

Choose specific integer types (`int64`) only when you have a specific reason (like hardware integration). Otherwise, use `int`.

← Go OutputGo Arrays →