Golang Type Assertions in 5 Minutes
Go type assertions for beginners, in five minutes, on a real Go program: why reading a JSON number with x.(int) panics with 'interface conversion: interface {} is float64, not int', what a value of type any really holds (a value and its type), the v, ok form that asks first instead of panicking, and a type switch for a value that could be several types.
The panic
Your Go program reads a plain thirty, and panics: float64, not int. A type assertion went wrong. In five minutes: why, and three safe ways out.

Chris runs a barber shop, and every Saturday morning his Go program loads the day's bookings, starting with Sarah at ten.

Chris reads the JSON into a map, a list of named values, each of type any, which can hold anything.
cat booking.json{"name": "Sarah", "time": "10:00", "minutes": 30}
sed -n '14p;19,20p' main.govar booking map[string]any mins := booking["minutes"].(int) fmt.Println("booked:", booking["name"], "for", mins, "minutes")
Then he takes the minutes value and writes dot, int in parentheses after it: a type assertion. It panics, and Saturday's list never loads.
go run . booking.jsonpanic: interface conversion: interface {} is float64, not int goroutine 1 [running]: main.main() /home/chris/shop/main.go:19 +0x1a4 exit status 2
What any holds
Thirty looks like a whole number, so why does Go say it isn't an int? First, two words: an int is a whole number, and a float64 can have a decimal point. Go's JSON reader turns every number into a float64. Inside an any, Go keeps two things: the value, and a label naming its type. A type assertion says: I'm sure this is an int, give it to me as one. Go checks the label. It doesn't say int, so Go panics.
![A value of type any carries its type: diagram of booking.json, booking["minutes"], type: float64, value: 30, .(int)](/img/golang-type-assertions-in-5-minutes/any.jpg)
Think of a parcel at the shop counter, with a packing slip naming what's inside. An assertion says: these are scissors, and opens the parcel. If the slip says comb, you stop right there.
Go's JSON package explains why: when JSON goes into an any, every number becomes a float64, even a whole number like thirty.

So Chris asserts the type that's really there: dot, float64. Run it, and Sarah's booking loads: thirty minutes.
sed -n '19,20p' main.gomins := booking["minutes"].(float64) fmt.Println("booked:", booking["name"], "for", mins, "minutes")
go run . booking.jsonbooked: Sarah for 30 minutes
Asking first
But Chris's phone app saves minutes as text, in quotes. Omar's booking: forty-five, as a string, Go's word for text. Same program, and it panics again: string, not float64.
cat phone.json{"name": "Omar", "time": "10:30", "minutes": "45"}
go run . phone.jsonpanic: interface conversion: interface {} is string, not float64 goroutine 1 [running]: main.main() /home/chris/shop/main.go:19 +0x1a8 exit status 2
Go's specification says it plainly: if a type assertion is false, a run-time panic occurs.

There's a second form that asks first. Write two names before the equals sign: mins and ok. Guess first: on Omar's booking, does this one panic too? No. ok is false, and mins is zero, the default value for a float64. It's reading the slip before you open the parcel. Chris checks ok, and skips the booking instead of crashing.
sed -n '19,24p' main.gomins, ok := booking["minutes"].(float64) fmt.Println("mins:", mins, "ok:", ok) if !ok { fmt.Println("not booked:", booking["name"], "- minutes is not a number") return }
go run . phone.jsonmins: 0 ok: false not booked: Omar - minutes is not a number
A type switch
But Omar did book. So Chris puts the check in a small function called minutes: its input, v, is the raw minutes value. Inside, he writes switch m colon-equals v dot, and the word type in parentheses. That's a type switch, for a value that could be one of several types.
sed -n '10,19p' main.gofunc minutes(v any) (int, error) { switch m := v.(type) { case float64: return int(m), nil case string: return strconv.Atoi(m) default: return 0, fmt.Errorf("minutes is %T", m) } }
Each case names a type, and inside that case, m has that type. For a float64, m is a float64, and int with m in parentheses converts it to a whole number: a conversion: it builds a new number, while an assertion only unwraps what's inside. For a string, m is text, and Go's strconv package has a function called Atoi, short for ASCII to integer, that turns text into a whole number. A last case, called default, catches any other type.
Saturday's file has three bookings. Sarah, thirty. Omar, forty-five. And Leo's has no minutes at all, so the value is nil, Go's word for nothing. Not a float64, not a string, so default says: check by hand.
cat saturday.json[ {"name": "Sarah", "time": "10:00", "minutes": 30}, {"name": "Omar", "time": "10:30", "minutes": "45"}, {"name": "Leo", "time": "11:15"} ]
go run . saturday.json10:00 Sarah - 30 minutes 10:30 Omar - 45 minutes 11:15 Leo - check by hand: minutes is <nil>
Saturday morning, every booking loads and nothing crashes. Sarah's in the chair, and Chris calls Leo to ask how long his cut should be.

Recap
So: a value of type any carries its type, and a plain type assertion panics if you name the wrong one. Three safe ways: assert the type that's really there, add ok to ask first, or use a type switch when it could be several.

If type assertions make sense now, subscribing brings the next Go idiom: generics.
The lab files and every command are linked in the description. Thanks for watching Cloud Explainers.
Sources
Demos: one container on our own Mac standing in for a laptop; the barber shop and its bookings are made up. Every command, output and number was real. Watch the full video on YouTube.