Variables & Types

Declare and initialize variables in Go

#variables #types #basics #declaration

Variables & Types

Go is a statically typed language with concise variable declaration.

Variable Declaration

// Explicit type
var name string = "Alice"
var age int = 30

// Type inference
var city = "New York"

// Short declaration (inside functions only)
count := 42
isActive := true

// Multiple variables
var x, y int = 1, 2
a, b := "hello", "world"

Basic Types

// Integers
var i int = 42
var u uint = 42

// Floats
var f float64 = 3.14

// Strings
var s string = "Hello, Go"

// Booleans
var active bool = true

// Type conversion
var x int = 42
var y float64 = float64(x)

Constants

const Pi = 3.14159
const AppName = "MyApp"

// Multiple constants
const (
    StatusOK = 200
    StatusNotFound = 404
    StatusError = 500
)

Discover another handy tool from EditPDF.pro