Go is a statically typed language, which means once you declare a variable with a certain type, it sticks with that type. It’s like declaring a secret identity for a superhero – Batman doesn’t suddenly become Spider-Man, right?.
Basically Types in GO can be classified into two.
1) Primitive types
2) Composite types
Primitive Types in GO
Here are the core primitive data types in Go:
- Integers:
int
,int8
,int16
,int32
,int64
(signed) - Unsigned Integers:
uint
,uint8
,uint16
,uint32
,uint64
,uintptr
- Floating-point numbers:
float32
,float64
- Complex numbers:
complex64
,complex128
- Booleans:
bool
- Strings:
string
- Rune: UTF-8 characters represented as integers (rune is an alias for int32)
Let’s see them in action!
package main
import "fmt"
func main() {
var age int = 30 // Integers for age
var pi float64 = 3.14159 // Float for precision
var isCodingFun bool = true // True or false, there’s no in-between!
var char rune = 'G' // A single character (Go uses Unicode)
fmt.Printf("Age: %d\n", age)
fmt.Printf("Pi: %f\n", pi)
fmt.Printf("Coding is fun: %t\n", isCodingFun)
fmt.Printf("First letter of Go: %c\n", char)
}
Output:
Age: 30
Pi: 3.141590
Coding is fun: true
First letter of Go: G
Composite Data Types in GO
Now that we know our basics, let’s take a ride through some composite data types:
- Arrays: Fixed-size collections of elements
- Slices: Arrays but cooler – they can grow!
- Maps: Key-value pairs
- Structs: Custom types, like Lego blocks for your data!
package main
import "fmt"
func main() {
// Arrays
var primes [5]int = [5]int{2, 3, 5, 7, 11}
// Slices
oddNumbers := []int{1, 3, 5, 7, 9}
// Maps (like a dictionary)
countryCodes := map[string]int{"USA": 1, "Canada": 1, "India": 91}
// Structs (your own custom data type)
type Person struct {
Name string
Age int
}
john := Person{Name: "John", Age: 25}
// Print them all!
fmt.Println("Prime numbers:", primes)
fmt.Println("Odd numbers:", oddNumbers)
fmt.Println("Country codes:", countryCodes)
fmt.Println("Person:", john)
}
Output:
Prime numbers: [2 3 5 7 11]
Odd numbers: [1 3 5 7 9]
Country codes: map[Canada:1 India:91 USA:1]
Person: {John 25}
Type Casting or Type Conversion in Golang
Go is strict about types, but sometimes you need to cast (convert) one type into another. It’s like turning a frog 🐸 into a prince 👑, but with data.
Here’s what you should know:
- Implicit conversions? Nope! Go doesn’t do implicit conversions between types. You have to do it yourself, like a wizard brewing a potion.
- Syntax for conversion:
Type(value)
. That’s it!
Convert Integer to Float in GO and Back
package main
import "fmt"
func main() {
var x int = 42
var y float64 = float64(x) // Convert int to float64
fmt.Printf("Integer to float: %f\n", y)
var z int = int(y) // Convert back to int
fmt.Printf("Float back to integer: %d\n", z)
}
Output:
Integer to float: 42.000000
Float back to integer: 42
Convert Integer to String in GO and Back
Go makes you work a bit to go between strings and other types. Here’s the go-to approach for converting strings to numbers (and vice versa) using the strconv
package.
package main
import (
"fmt"
"strconv"
)
func main() {
// Convert string to int
str := "100"
num, _ := strconv.Atoi(str) // _ ignores the error for now
fmt.Printf("String to int: %d\n", num)
// Convert int to string
strNum := strconv.Itoa(num)
fmt.Printf("Int to string: %s\n", strNum)
}
Output:
String to int: 100
Int to string: 100
Conversion Pitfalls to Watch For in GO
Precision loss: When converting from
float64
toint
, you lose everything after the decimal.3.14
becomes3
– those poor decimals are sacrificed. 😢
Overflowing integers: Trying to fit a bigger number into a smaller type can cause overflow. For example, an
int64
value won’t fit into anint8
type.- Imagine trying to stuff a giant sandwich into a small lunchbox. It’s going to spill! 🥪
You can find an entire video on type conversion and more in my Youtube Channel.