Learn how to declare variables and work with Go's basic data types
Learning Objectives
Declare variables using var and short declaration syntax
Understand Go's basic data types
Work with zero values and type inference
Use constants for fixed values
Variables in Go
Variables are containers that store data. In Go, every variable has a specific type that determines what kind of data it can hold.
Static Typing
Go is statically typed, meaning variable types are determined at compile time. This catches many errors before your program runs!
Declaring Variables
Go provides several ways to declare variables.
Using var Keyword
The most explicit way to declare a variable:
main.go
1
2
3
4
5
6
7
8
9
10
11
package main
import"fmt"funcmain() {
var name string = "Gopher"var age int = 25var isActive bool = true fmt.Println(name, age, isActive)
}
Type Inference
Go can infer the type from the value:
main.go
1
2
3
var name = "Gopher"// Go infers stringvar age = 25// Go infers intvar price = 19.99// Go infers float64
Short Declaration (:=)
Inside functions, you can use the short declaration syntax:
main.go
1
2
3
4
5
6
7
8
9
10
11
package main
import"fmt"funcmain() {
name :="Gopher"// Short declaration age :=25 isActive :=true fmt.Println(name, age, isActive)
}
Important
The := syntax can only be used inside functions. At package level, you must use var.
Multiple Variable Declaration
Declare multiple variables at once:
main.go
1
2
3
4
5
6
7
8
9
10
11
12
// Multiple variables of same typevar x, y, z int = 1, 2, 3// Multiple variables of different typesvar (
name string = "Gopher" age int = 25 balance float64 = 100.50)
// Short declaration for multiple variablesa, b, c :=1, "hello", true
Zero Values
In Go, variables declared without an initial value get a zero value:
Type
Zero Value
int, int8, int16, int32, int64
0
uint, uint8, uint16, uint32, uint64
0
float32, float64
0.0
bool
false
string
"" (empty string)
pointer, slice, map, channel, function, interface
nil
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
package main
import"fmt"funcmain() {
var i intvar f float64var b boolvar s string fmt.Printf("int: %d, float: %f, bool: %t, string: %q\n", i, f, b, s)
// Output: int: 0, float: 0.000000, bool: false, string: ""}
Basic Data Types
Go has several built-in data types organized into categories:
package main
import"fmt"funcmain() {
var a int = 42var b int8 = 127var c uint = 100var d int64 = 9223372036854775807 fmt.Printf("a: %d, b: %d, c: %d, d: %d\n", a, b, c, d)
}
Which int to use?
Use int for most cases - it’s the most common and efficient. Use specific sizes (int8, int32, etc.) only when you need to control memory usage or match external data formats.
Floating-Point Types
main.go
1
2
3
4
5
6
7
8
9
10
11
package main
import"fmt"funcmain() {
var f32 float32 = 3.14159var f64 float64 = 3.141592653589793 fmt.Printf("float32: %f\n", f32)
fmt.Printf("float64: %.15f\n", f64)
}
package main
import"fmt"funcmain() {
var isGo bool = truevar isFun bool = true fmt.Println("Go is awesome:", isGo && isFun)
}
String Type
Strings in Go are immutable sequences of bytes (usually UTF-8 text):
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main
import"fmt"funcmain() {
greeting :="Hello, Go!" multiline :=`This is a
multi-line string
using backticks` fmt.Println(greeting)
fmt.Println(multiline)
fmt.Println("Length:", len(greeting))
}
Byte and Rune
byte - alias for uint8, represents a single byte
rune - alias for int32, represents a Unicode code point
main.go
1
2
3
4
5
6
7
8
9
10
11
package main
import"fmt"funcmain() {
var b byte = 'A'// ASCII value 65var r rune = 'δΈ–'// Unicode code point fmt.Printf("byte: %c (%d)\n", b, b)
fmt.Printf("rune: %c (%d)\n", r, r)
}
Constants
Constants are values that don’t change. Use the const keyword:
Go requires explicit type conversion (no automatic conversion):
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main
import"fmt"funcmain() {
var i int = 42var f float64 = float64(i) // int to float64var u uint = uint(f) // float64 to uint fmt.Printf("int: %d, float64: %f, uint: %d\n", i, f, u)
// String conversionvar num int = 65var char string = string(num) // Converts to "A" (ASCII) fmt.Println(char)
}
No Implicit Conversion
Go does not automatically convert between types, even between int and int64. You must explicitly convert using Type(value).
Printf Format Verbs
Use fmt.Printf with format verbs for formatted output:
Verb
Description
%d
Decimal integer
%f
Floating-point
%s
String
%t
Boolean
%v
Default format
%T
Type of value
%q
Quoted string
%c
Character (rune)
%b
Binary
%x
Hexadecimal
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main
import"fmt"funcmain() {
name :="Gopher" age :=10 height :=1.75 fmt.Printf("Name: %s\n", name)
fmt.Printf("Age: %d years\n", age)
fmt.Printf("Height: %.2f meters\n", height)
fmt.Printf("Type of name: %T\n", name)
fmt.Printf("Default format: %v, %v, %v\n", name, age, height)
}
Summary
Today you learned:
How to declare variables using var and :=
Go’s basic data types (integers, floats, bools, strings)
Zero values and type inference
How to use constants and iota
Explicit type conversion in Go
Printf format verbs for output
Practice Exercise
Create variables of different types and print them
Experiment with zero values - declare variables without values
Create a set of constants for HTTP status codes using iota
Practice type conversion between int and float64
Next Steps
Tomorrow, we’ll explore operators and expressions - how to perform calculations and comparisons in Go!
Now, complete the quiz below to test your knowledge and unlock Day 3!
Quiz
Complete this quiz with a minimum score of 80% to mark Day 2 as complete.
Loading quiz...
Discussion
Have questions or want to discuss this lesson? Join the conversation below!
Settings
Appearance
Cloud Sync
Sync StatusChecking...
Link or Sign In to sync progress across devices
Data
This will delete all your progress for this course
Discussion
Have questions or want to discuss this lesson? Join the conversation below!