Variable Declaration
Go is a statically typed language requiring explicit type definitions. The var keyword declares variables.
Syntax and Conventions
Declare a varible with var name type.
- Naming follows camelCase: lowercase enitial letter, uppercase for new words.
var username string = "Alice"
username = "Bob"
Declare multiple variables together:
var (
employeeCount int
departmentName string
)
Declare several variables of the same type:
var width, height, depth int
Default Values
Uninitialized variables receive zero values:
- Numeric types:
0or0.0 - Strings: empty string
"" - Booleans:
false - Slices, functions, pointers:
nil
Initialization
Standard Format
var appName string = "ServiceApp"
var port = 8080
Short Assignment
Use := for concise initialization. The compiler enfers the type. This syntax is restricted to function bodies.
configPath := "/etc/app/config.yaml"
maxRetries := 3
Memory Address
Print a variable's value and memory address:
counter := 42
fmt.Printf("Value: %d, Address: %p\n", counter, &counter)
Value Swapping
Swap two variables without a temporary variable:
posX := 10
posY := 20
posY, posX = posX, posY
Anonymous Variables
The blank identifier _ discards values. It does not allocate memory and can be reused.
defaultPort, _ := getConfig() // Ignoring second return value
Scope
Variables have block-level scope. Local variables shadow global ones with the same name.
var logLevel = "INFO"
func main() {
logLevel := "DEBUG" // Local variable
fmt.Println(logLevel) // Outputs "DEBUG"
}
Global variables are accessible across packages after import.
Constants
Constants are immutable values declared with const.
Definition
Explicit and implicit typing are supported. Group related constants.
const siteName string = "Example.com"
const maxUsers = 1000
const (
statusOK = 200
statusNotFound = 404
statusError = 500
)
In a group, an uninitialized constant takes the value of the previous one.
const (
a = 5
b // b is 5
c = 10
d // d is 10
)
Iota
iota is a constant generator that increments with each line in a const block, starting at 0.
const (
bit0 = iota // 0
bit1 // 1
bit2 // 2
)
const (
flagA = iota // 0
flagB // 1
flagC = "on" // "on", iota is 2
flagD // "on", iota is 3
flagE = iota // 4
)
Boolean Type
The bool type represents truth values. Defaults to false.
var isEnabled bool
fmt.Printf("Type: %T, Value: %t\n", isEnabled, isEnabled) // Type: bool, Value: false
String Type
Strings are sequences of characters enclosed in double quotes. Single quotes denote a rune (Unicode code point).
message := "Hello, World"
letter := 'A' // Rune, prints as 65
Concatenation
Join strings with the + operator.
fullMessage := "Status: " + "OK"
Escape Sequences
\"for double quote\\for backslash\nfor newline\tfor tab
Type Conversion
Go requires explicit type conversion. Wrap the target type in parentheses.
temperature := 23.5
intTemp := int(temperature) // 23, fractional part lost
Converting between incompatible types, like numeric to boolean, causes a compilation error.