Variable Declaration
Variables in Go are declared using the var keyword followed by the variable name and type. The language supports both individual and batch declaration formats.
var age int
var name string
var isValid bool
// Batch declaration
var (
width float64
height float64
title string
)
Go automatically initializes declared variables with zero values:
- Integers: 0
- Floating-point numbers: 0.0
- Strings: "" (empty string)
- Boolean: false
- Slices, maps, functions, and pointers: nil
Variable Initialization
Variables can be initialized during declaration using several syntax options:
// Standard initialization
var counter int = 10
var message string = "Hello"
// Type inference
var score = 95 // int inferred
var price = 29.99 // float64 inferred
var active = true // bool inferred
// Short declaration (inside functions)
quantity := 5
username := "john_doe"
Variable Assignment and Swapping
Go supports multiple assignment and value swapping without temporary variables:
// Multiple assignment
x, y, z := 10, 20, 30
// Value swapping
a, b := 100, 200
a, b = b, a // a becomes 200, b becomes 100
Anonymous Variables
The underscore _ serves as an anonymous variable placeholder for unused return values:
func getUserData() (string, int) {
return "Alice", 25
}
name, _ := getUserData() // Ignore age return value
Variable Scope
Variable visibility follows block-level scoping rules:
package main
var globalVar = "accessible everywhere"
func demoFunction() {
localVar := "only accessible within this function"
if true {
blockVar := "only accessible within this if block"
println(blockVar)
}
// println(blockVar) // Error: undefined
}
Basic Data Types
Integer Types
Go provides signed and unsigned integers in various sizes:
var num8 int8 = 127 // 8-bit integer (-128 to 127)
var num16 int16 = 32767 // 16-bit integer
var num32 int32 = 2147483647
var num64 int64 = 9223372036854775807
var unum8 uint8 = 255 // Unsigned 8-bit integer (0 to 255)
var unum16 uint16 = 65535
var byteAlias byte = 255 // alias for uint8
var runeChar rune = 'A' // alias for int32, represents Unicode
Floating-Point Types
Go supports two floating-point precision levels:
var float32Val float32 = 3.14159
var float64Val float64 = 2.718281828459045
String Type
Strings are immutable sequences of bytes represanting UTF-8 encoded text:
var greeting string = "Hello, World!"
var multiline = `This is a
multi-line
string literal`
// String operations
str1 := "Hello"
str2 := "World"
result := str1 + " " + str2 // Concatenation
length := len(result) // Byte length
Boolean Type
The boolean type represents true/false values:
var isReady bool = true
var isEmpty = false
Type Checking and Conversion
Type Identification
Use the fmt package with %T format specifier to check variable types:
import "fmt"
var value int = 42
fmt.Printf("Type: %T\n", value) // Output: Type: int
Type Conversion
Go requires explicit type conversion between different types:
var integer int = 100
var floatNumber float64 = float64(integer)
var stringRep string = string(integer) // Converts to character, not "100"
// Proper numeric to string conversion
import "strconv"
strValue := strconv.Itoa(integer) // "100"
Pointer Types
Pointers store memory addresses of variables:
var original int = 50
var pointer *int = &original // & gets address
fmt.Println(*pointer) // * dereferences pointer, outputs: 50
*pointer = 100 // Modifies original variable
fmt.Println(original) // Outputs: 100