Go Lesson 10 – Type Conversion| Dataplexa

Type Conversion in Go

In Go, variables are strictly typed. This means a value of one data type cannot be used as another data type directly.

To work with different data types together, Go requires explicit type conversion. This design prevents unexpected errors and makes programs safer.


Why Type Conversion Is Required

Unlike some programming languages that automatically convert types, Go requires the programmer to clearly specify conversions.

This avoids silent bugs and improves code reliability.


Basic Type Conversion Syntax

The general syntax for type conversion in Go is:

newType(value)

Here, newType is the target data type.


Converting Integer to Float

Integers and floating-point numbers are different types in Go. You must convert explicitly before mixing them.

package main

import "fmt"

func main() {
    var a int = 10
    var b float64 = 3.5

    result := float64(a) + b
    fmt.Println("Result:", result)
}

Here, a is converted to float64 so it can be added to b.


Converting Float to Integer

When converting a float to an integer, Go removes the decimal part. It does not round the number.

package main

import "fmt"

func main() {
    price := 99.99
    rounded := int(price)

    fmt.Println("Original:", price)
    fmt.Println("Converted:", rounded)
}

The decimal portion is discarded, not rounded.


Converting Between Numeric Types

Go treats all numeric types as distinct. For example, int, int64, and float32 are not interchangeable.

package main

import "fmt"

func main() {
    var x int64 = 500
    var y int = int(x)

    fmt.Println("x:", x)
    fmt.Println("y:", y)
}

Converting String to Number

To convert strings into numbers, Go uses the strconv package.

This is common when reading user input or file data.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    text := "150"
    number, _ := strconv.Atoi(text)

    fmt.Println("String:", text)
    fmt.Println("Number:", number)
}

Atoi converts a string into an integer.


Converting Number to String

Sometimes you need to convert numeric values into strings for display or logging.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    value := 2025
    text := strconv.Itoa(value)

    fmt.Println("Number:", value)
    fmt.Println("String:", text)
}

Real-World Example: Billing System

Let’s combine multiple type conversions in a real billing scenario.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    priceText := "49.99"
    quantity := 3

    price, _ := strconv.ParseFloat(priceText, 64)
    total := price * float64(quantity)

    fmt.Println("Total bill:", total)
}

This example converts string input to float, then performs calculations safely.


Common Type Conversion Errors

  • Forgetting explicit conversion
  • Converting float to int expecting rounding
  • Ignoring conversion errors from strconv

Best Practices

  • Always convert explicitly
  • Be careful when losing decimal precision
  • Handle conversion errors in real applications

Why Go Enforces Type Conversion

Explicit type conversion makes Go programs:

  • Safer
  • Easier to debug
  • More predictable in large systems

What’s Next?

In the next lesson, you will learn conditional statements in Go using if, else if, and else.