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:

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: %dn", age)
    fmt.Printf("Pi: %fn", pi)
    fmt.Printf("Coding is fun: %tn", isCodingFun)
    fmt.Printf("First letter of Go: %cn", 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:

				
					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:

  1. Implicit conversions? Nope! Go doesn’t do implicit conversions between types. You have to do it yourself, like a wizard brewing a potion. 
  2. 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: %fn", y)
    var z int = int(y) // Convert back to int
    fmt.Printf("Float back to integer: %dn", z)
}

				
			

WATCH OUT
When converting from float to int, Go truncates the value (cuts off the decimal part). No rounding here, just a clean chop! ✂️

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: %dn", num)
    // Convert int to string
    strNum := strconv.Itoa(num)
    fmt.Printf("Int to string: %sn", strNum)
}

				
			

Output:

				
					String to int: 100
Int to string: 100
				
			

Conversion Pitfalls to Watch For in GO

You can find an entire video on type conversion and more in my Youtube Channel.

https://youtu.be/6EtEaw9PYh8

Stay tuned for more Go adventures. Type responsibly, and happy coding!