Go Curso

Go Variables and Data Types

Tutorial de Python 3 para principiantes.

Declaring Variables

Go is statically typed — every variable has a type known at compile time. There are two main ways to declare variables:

// Using var keyword (explicit type)
var name string = "Alice"
var age int = 30
var price float64 = 9.99

// Short declaration := (type inferred)
name := "Alice"
age := 30
price := 9.99

Multiple variable declaration

var (
    firstName string = "John"
    lastName  string = "Doe"
    score     int    = 100
)

// Or with short declaration
x, y, z := 1, 2, 3

Basic Data Types

var i int = 42           // integer (int8, int16, int32, int64)
var f float64 = 3.14     // floating point
var b bool = true        // boolean
var s string = "hello"   // string (UTF-8)
var r rune = 'A'        // rune = int32 (Unicode code point)
var by byte = 255        // byte = uint8

Constants

const Pi = 3.14159
const AppName = "MyApp"
const MaxRetries = 3

// Iota — auto-incrementing constant
const (
    Sunday = iota  // 0
    Monday         // 1
    Tuesday        // 2
    Wednesday      // 3
)

Zero values

In Go, variables declared without an explicit initial value are given their zero value:

var i int     // 0
var f float64 // 0.0
var b bool    // false
var s string  // ""