Conversion, Not Casting

Part of Golang Mastery course

~15 min read
Interactive
Hands-on
Beginner-friendly

Conversion means we take the value of one type and convert it to another type.

Let's try it out in the Go playground

example.go
package main
 
import (
string">"fmt"
)
 
var a int
type hotdog int
var b hotdog
 
func main() {
a = 42
b = hotdog(a) string">"comment">// we can convert the value of a to a value of type hotdog
fmt.Println(a)
fmt.Printf(string">"%T\n", a)
fmt.Println(b)
fmt.Printf(string">"%T\n", b)
}
 

In other programming languages, this is called casting. We don't call it casting in Go, we call it conversion. If you go to Effective Go and search for "cast" you won't find any results, but if you search for "conversion" you will.

That's the end of this section!

Your Progress

31 of 103 modules
30%
Started30% Complete
Previous
SpaceComplete
Next