Variables and Types
## Learning Objectives
- Declare and initialize variables
- Understand Go's basic types
- Master type inference
- Work with constants
- Zero values and default behavior
## Variables
### Declaration
```go
var name string = "Go" // Declaration with type
var version = 1.20 // Declaration with inference
name := "Golang" // Short declaration (functions only)
```
### Declaring Multiple Variables
```go
var (
name string = "Go"
version float64 = 1.20
isAwesome bool = true
)
```
### Parallel Declaration
```go
var a, b, c int = 1, 2, 3
var name, age = "Alice", 30
```
## Naming Rules
- Start with letter or underscore
- Can contain numbers (after first character)
- Case-sensitive
- Cannot use reserved words
```go
var name string // Valid
var _count int // Valid (private)
var 2ndPlace int // Invalid
var for int // Invalid (reserved word)
```
## Basic Types
### Integer Types
| Type | Range |
|------|-------|
| int | Platform-dependent (32 or 64 bit) |
| int8 | -128 to 127 |
| int16 | -32,768 to 32,767 |
| int32 | -2,147,483,648 to 2,147,483,647 |
| int64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| uint | Unsigned platform-dependent |
| uint8 | 0 to 255 |
| uint16 | 0 to 65,535 |
| uint32 | 0 to 4,294,967,295 |
| uint64 | 0 to 18,446,744,073,709,551,615 |
```go
var i int = 100
var ui uint = 100
```
### Floating-Point Types
| Type | Range |
|------|-------|
| float32 | ~1.4e-45 to ~3.4e38 |
| float64 | ~4.9e-324 to ~1.8e308 |
```go
var pi float64 = 3.14159265359
var e float32 = 2.71828
```
### Complex Numbers
```go
var c1 complex64 = 1 + 2i
var c2 complex128 = 1 + 2i
```
### Boolean Type
```go
var isActive bool = true
var hasPermission bool = false
```
### String Type
```go
var name string = "Go Programming"
var empty string = ""
```
### Rune Type
```go
var grade rune = 'A' // Unicode code point
var emoji rune = '😀'
```
### Byte Type
```go
var b byte = 'A' // Alias for uint8
```
## Type Inference
```go
var version = 1.20 // Inferred as float64
var count = 100 // Inferred as int
var name = "Go" // Inferred as string
var isTrue = true // Inferred as bool
```
## Zero Values
Variables declared without initialization get a zero value.
| Type | Zero Value |
|------|------------|
| int | 0 |
| float64 | 0.0 |
| bool | false |
| string | "" |
| pointers | nil |
```go
var i int // 0
var f float64 // 0.0
var b bool // false
var s string // ""
var arr []int // nil
```
## Constants
### Constant Declaration
```go
const Pi = 3.14159
const MaxSize = 100
const AppName = "MyApp"
```
### Multiple Constants
```go
const (
StatusOK = 200
StatusError = 500
StatusRedirect = 300
)
```
### iota Enumerator
```go
const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
Wednesday // 3
)
const (
Flag1 = 1 << iota // 1
Flag2 // 2
Flag3 // 4
)
```
## Type Conversion
### Explicit Conversion Required
Go does not allow implicit type conversion.
```go
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
```
### Integer to String
```go
import "strconv"
i := 42
s := strconv.Itoa(i) // "42"
s := strconv.FormatInt(int64(i), 10)
```
### String to Integer
```go
import "strconv"
s := "42"
i, err := strconv.Atoi(s) // 42, nil
```
### ParseFloat
```go
import "strconv"
s := "3.14159"
f, err := strconv.ParseFloat(s, 64) // 3.14159
```
## String Operations
### Length
```go
import "unicode/utf8"
s := "Hello"
len(s) // 5 (bytes)
utf8.RuneCountInString(s) // 5 (characters)
```
### Concatenation
```go
s1 := "Hello"
s2 := "World"
s3 := s1 + " " + s2 // "Hello World"
```
### String Builder
```go
import "strings"
var sb strings.Builder
sb.WriteString("Hello")
sb.WriteString(" ")
sb.WriteString("World")
s := sb.String() // "Hello World"
```
## Variables Scope
### Function Scope
```go
func main() {
x := 10 // Function scope
if x > 5 {
y := 20 // Block scope
fmt.Println(x, y)
}
// fmt.Println(y) // Error: y is undefined
}
```
### Package Scope
```go
var globalVar = "I am global"
func main() {
fmt.Println(globalVar)
}
```
## Redeclaration
Short declaration can redeclare variables in the same scope.
```go
i, j := 1, 2
i, j := 3, 4 // Redeclares i and j
```
## Blank Identifier
Discard values using `_`.
```go
result, _ := someFunction() // Ignore second return value
```
## Summary
- Variables declared with `var` or `:=`
- Go has multiple integer types with different ranges
- Floating-point: `float32` and `float64`
- Boolean: `true` or `false`
- String: UTF-8 encoded characters
- Zero values: `0`, `0.0`, `false`, `""`, `nil`
- Constants declared with `const`
- Type conversion must be explicit: `float64(i)`
- Go does not allow implicit type conversion
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →