Go Type Conversion

Go never converts types automatically. Every type conversion must be written explicitly in the code. This design prevents hidden bugs that occur in languages where values silently change type behind the scenes.

What Is Type Conversion?

Type conversion takes a value of one type and produces a new value of a different type. The original variable does not change — a new value gets created in the target type.

Type Conversion Syntax:

targetType(value)

Example:
int(3.7)        → converts float64 to int  → result: 3
float64(10)     → converts int to float64  → result: 10.0
string(65)      → converts int to string   → result: "A"

Integer and Float Conversion

package main

import "fmt"

func main() {
    var x int = 10
    var y float64 = float64(x)  // int → float64

    var a float64 = 9.7
    var b int = int(a)           // float64 → int (decimal is dropped, not rounded)

    fmt.Println(y) // 10
    fmt.Println(b) // 9
}

Important: Converting float to int drops the decimal portion entirely. It does not round the number. int(9.9) gives 9, not 10.

Conversion Between Integer Sizes

package main

import "fmt"

func main() {
    var small int8 = 100
    var large int64 = int64(small)  // int8 → int64

    fmt.Println(large) // 100
}

Integer to String Conversion

Converting an integer directly to string using string(n) gives the Unicode character for that number, not the digit as text. To get the digit as text, use the strconv package.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    n := 42

    // This gives the Unicode character, not the text "42"
    fmt.Println(string(n)) // "*"  ← not what most people want

    // This gives the string "42"
    s := strconv.Itoa(n)
    fmt.Println(s) // "42"
}

String to Integer Conversion

Use strconv.Atoi to convert a string that contains a number into an actual integer. It returns two values: the result and an error.

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "100"
    n, err := strconv.Atoi(s)

    if err != nil {
        fmt.Println("Conversion failed:", err)
    } else {
        fmt.Println(n + 5) // 105
    }
}

String to Float Conversion

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "3.14"
    f, err := strconv.ParseFloat(s, 64)

    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println(f * 2) // 6.28
    }
}

Conversion Reference Table

FromToMethod
intfloat64float64(n)
float64intint(f) — drops decimal
intstring (text)strconv.Itoa(n)
stringintstrconv.Atoi(s)
stringfloat64strconv.ParseFloat(s, 64)
float64stringstrconv.FormatFloat(f, 'f', 2, 64)
intint64int64(n)
bytestringstring([]byte{b})

Common Mistake – Mixing Types Without Conversion

package main

import "fmt"

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

    // This causes a compile error:
    // fmt.Println(a + b)   ← cannot mix int and float64

    // Correct approach:
    fmt.Println(float64(a) + b) // 13.5
}

Key Points

  • Go never converts types automatically — every conversion is explicit
  • Use targetType(value) syntax for basic numeric conversions
  • Converting float to int drops the decimal — it does not round
  • Use strconv.Itoa and strconv.Atoi for number-string conversions
  • Mixing types in expressions (like int + float64) causes a compile error

Leave a Comment