Go — Data Types
Basic types
| Type | Description |
|---|---|
bool | true or false |
string | UTF-8 text |
int, int8, int16, int32, int64 | Signed integers |
uint, uint8, uint16, uint32, uint64 | Unsigned integers |
float32, float64 | Floating point |
complex64, complex128 | Complex numbers |
byte | Alias for uint8 |
rune | Alias for int32 (Unicode code point) |
Numeric types
package main
import "fmt"
func main() {
var i int = 42
var f float64 = 3.14
var u uint = 42
var b byte = 255
var r rune = 'A'
fmt.Println(i, f, u, b, r)
}
Strings
package main
import "fmt"
func main() {
s := "Hello, World!"
raw := `Raw string
with newlines`
utf8 := "UTF-8: 你好世界"
fmt.Println(s)
fmt.Println(raw)
fmt.Println(utf8)
fmt.Println(len(s)) // 13
fmt.Println(s[0:5]) // Hello
fmt.Println(s + " Bye") // Hello, World! Bye
}
Arrays
package main
import "fmt"
func main() {
var arr [5]int
arr[0] = 10
arr[1] = 20
fruits := [3]string{"Apple", "Banana", "Cherry"}
fmt.Println(arr) // [10 20 0 0 0]
fmt.Println(fruits) // [Apple Banana Cherry]
fmt.Println(len(arr)) // 5
}
Slices
package main
import "fmt"
func main() {
nums := []int{1, 2, 3, 4, 5}
slice := nums[1:3] // [2, 3]
fmt.Println(nums) // [1 2 3 4 5]
fmt.Println(slice) // [2 3]
// Append
nums = append(nums, 6, 7)
// Make
data := make([]int, 5, 10) // len=5, cap=10
fmt.Println(data)
}
Maps
package main
import "fmt"
func main() {
ages := map[string]int{
"Alice": 30,
"Bob": 25,
}
ages["Charlie"] = 35
delete(ages, "Bob")
fmt.Println(ages)
// Check if key exists
age, ok := ages["Alice"]
if ok {
fmt.Println("Alice:", age)
}
}
Structs
package main
import "fmt"
type Person struct {
Name string
Age int
}
func main() {
p1 := Person{Name: "Alice", Age: 30}
p2 := Person{"Bob", 25} // Positional
fmt.Println(p1.Name)
fmt.Println(p2)
}
Pointers
package main
import "fmt"
func increment(x *int) {
*x++
}
func main() {
x := 10
increment(&x)
fmt.Println(x) // 11
}
Type conversion
package main
import (
"fmt"
"strconv"
)
func main() {
// Numeric conversion
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
fmt.Println(i, f, u)
// String conversion
s := strconv.Itoa(i) // "42"
n, _ := strconv.Atoi("42") // 42
fmt.Println(s, n)
}
Mini Practice
Write Go code that:
- Declares variables of each basic type
- Creates a slice and appends elements
- Creates a map and iterates with range
- Defines a struct with fields and methods
Up Next
In the next lesson, you'll learn about Operators — arithmetic and logical operations.
Related Topics
Frequently Asked Questions about Data Types
What is Data Types in Go?
Data Types is a fundamental concept in Go. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Data Types?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Data Types.
Why is Data Types important in Go?
Data Types is essential for Go development. Understanding this concept will help you write better code and solve real-world problems more effectively.