Menu
Coddy logo textTech

Type Switch

Part of the Logic & Flow section of Coddy's GO journey — lesson 18 of 68.

While type assertions work well for checking a single type, you often need to handle multiple possible types for an interface variable. This is where a type switch becomes incredibly useful - it provides a clean way to perform different actions based on the concrete type.

A type switch uses this special syntax: switch v := i.(type). Notice the (type) keyword instead of a specific type. The variable v will contain the actual value with its concrete type in each case:

func describe(data interface{}) {
    switch v := data.(type) {
    case int:
        fmt.Printf("Integer: %d\n", v)
    case string:
        fmt.Printf("String: %s\n", v)
    case bool:
        fmt.Printf("Boolean: %t\n", v)
    default:
        fmt.Printf("Unknown type: %T\n", v)
    }
}

In each case, v automatically has the correct type - no additional type assertion needed. This makes type switches much cleaner than chaining multiple type assertions with if-else statements, especially when you need to handle several different types in one place.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

In this challenge, you'll practice using type switches to handle multiple data types in a notification system. You'll create a function that processes different types of notifications and formats them appropriately based on their concrete type.

You will receive two inputs:

  • A string representing the notification type (e.g., "email", "sms", "push", or "alert")
  • A string representing the notification content or value

Your task is to:

  1. Create a function called processNotification that takes an interface{} parameter
  2. Based on the notification type input, convert the content to the appropriate Go type and store it in an interface{} variable:
    • For "email": use the content as a string
    • For "sms": convert the content to an integer (representing character count)
    • For "push": convert the content to a boolean (true for "enabled", false for anything else)
    • For "alert": convert the content to a float64 (representing priority level)
  3. Call the processNotification function with the converted value
  4. Inside processNotification, use a type switch to handle each type and print the appropriate message:
    • For string: "Email notification: [value]"
    • For int: "SMS notification with [value] characters"
    • For bool: "Push notifications: [value]"
    • For float64: "Alert with priority: [value]"
    • For any other type: "Unknown notification type"

Use the strconv package for string conversions. For boolean conversion, only "enabled" should result in true. This challenge demonstrates how type switches provide a clean way to handle multiple types in a single control structure, automatically giving you the correctly typed value in each case.

Cheat sheet

A type switch provides a clean way to handle multiple possible types for an interface variable using the syntax switch v := i.(type):

func describe(data interface{}) {
    switch v := data.(type) {
    case int:
        fmt.Printf("Integer: %d\n", v)
    case string:
        fmt.Printf("String: %s\n", v)
    case bool:
        fmt.Printf("Boolean: %t\n", v)
    default:
        fmt.Printf("Unknown type: %T\n", v)
    }
}

In each case, the variable v automatically has the correct concrete type, eliminating the need for additional type assertions.

Try it yourself

package main

import (
	"fmt"
	"strconv"
)

func main() {
	// Read input
	var notificationType string
	var content string
	fmt.Scanln(&notificationType)
	fmt.Scanln(&content)
	
	// TODO: Write your code below
	// 1. Create processNotification function that takes interface{} parameter
	// 2. Convert content to appropriate type based on notificationType
	// 3. Call processNotification with the converted value
	// 4. Use type switch inside processNotification to handle different types
	
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow