Skip to content
Justice Tech School

Type Switch and fallthrough

A type switch checks the dynamic type held by an interface. fallthrough forces the next case to run.

Example
var x interface{} = 3.5
switch v := x.(type) {
case int:
	fmt.Println("int", v)
case float64, float32:
	fmt.Println("float", v)
case string:
	fmt.Println("string", v)
default:
	fmt.Println("other")
}
switch n := 2; {
case n > 1:
	fmt.Println("big")
	fallthrough
case n > 100:
	fmt.Println("fell through")
}

Result

float 3.5
big
fell through

Exercise

Check the dynamic type.

switch v := x.() {

Quiz Test

What does fallthrough do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.