Type Conversions in Go
Explicit conversions, type aliases and defined types
Type Conversions in Go is a free Go Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Explicit Conversions?
Go never implicitly converts between types. This prevents subtle bugs where a small integer silently overflows a larger one or a float loses precision.
- Conversions must be written explicitly:
T(value) - The compiler catches mismatched types at compile time
- This makes Go code predictable and safe
Numeric Conversions
Convert between numeric types with the target type as a function call:
package main
func main() {
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
_ = u
}Truncation Risk
Converting from a larger to a smaller type truncates the value. No error is raised — you must be careful:
package main
func main() {
big := int64(300)
small := int8(big) // 300 % 256 = 44 — silent truncation!
_ = small
}string and []byte Conversions
Converting between string and []byte copies the data:
package main
func main() {
s := "hello"
b := []byte(s) // string to byte slice
b[0] = 'H'
s2 := string(b) // byte slice to string
_ = s2
}string and []rune
For Unicode-safe character manipulation, convert to []rune:
package main
func main() {
s := "Merhaba"
runes := []rune(s)
_ = len(runes) // character count, not byte count
}strconv Package
For converting between strings and numbers, use the strconv package:
import "strconv"
func main() {
n, err := strconv.Atoi("42") // string -> int
s := strconv.Itoa(123) // int -> string
_ = n; _ = err; _ = s
}strconv.ParseFloat and FormatFloat
Parse and format floating-point strings safely:
import "strconv"
func main() {
f, err := strconv.ParseFloat("3.14", 64)
if err != nil { return }
s := strconv.FormatFloat(f, 'f', 2, 64) // "3.14"
_ = s
}Type Aliases vs Defined Types
Go distinguishes between type aliases and defined types:
type Celsius float64— new defined type; needs explicit conversion to/fromfloat64type Celsius = float64— alias; interchangeable withfloat64
Defined Type Example
Defined types improve API clarity and prevent accidental mixing:
type Celsius float64
type Fahrenheit float64
func CtoF(c Celsius) Fahrenheit {
return Fahrenheit(c*9/5 + 32)
}
func main() {
boiling := Celsius(100)
_ = CtoF(boiling)
}Interface Conversions
Any value can be converted to an interface type it satisfies. Converting back requires a type assertion:
var i interface{} = "hello"
s, ok := i.(string) // type assertion
if ok {
_ = s // safe to use
}Quick Check
What function converts a string to an int in Go?
Recap: Type Conversions
Key takeaways for Go type conversions:
- Go never implicitly converts — use
T(value)syntax - Numeric conversions may truncate — be explicit about the risk
[]byte(s)andstring(b)copy data- Use
strconv.Atoi/strconv.Itoafor string-number conversions - Defined types need explicit conversions; aliases do not
Next course: Control Flow with if, for, and switch.
Frequently asked questions
Is the “Type Conversions in Go” lesson free?
Yes — the full text of “Type Conversions in Go” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Type Conversions in Go”?
Explicit conversions, type aliases and defined types You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Type Conversions in Go” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Variables in Go
- Go's Built-in Types
- Constants and iota
- Type Conversions in Go