ZetCode

Golang default keyword

last modified May 7, 2025

This tutorial explains how to use the default keyword in Go. We'll cover switch statement basics with practical examples of default cases.

The default case in a switch statement executes when no other cases match. It's optional but useful for handling unexpected or fall-through values.

In Go, default can appear anywhere in a switch statement, though convention places it at the end. It ensures all possible values are handled.

Basic default case

The simplest use of default provides a fallback when no cases match. This example demonstrates its basic syntax and behavior.

basic_default.go
package main

import "fmt"

func main() {
    day := "Tuesday"
    
    switch day {
    case "Monday":
        fmt.Println("Start of work week")
    case "Friday":
        fmt.Println("Almost weekend")
    default:
        fmt.Println("Midweek day")
    }
}

Since "Tuesday" doesn't match any case, the default block executes. The output will be "Midweek day".

Default with numeric values

default works with all types. This example shows its use with integer values in a switch statement.

numeric_default.go
package main

import "fmt"

func main() {
    age := 42
    
    switch {
    case age < 18:
        fmt.Println("Minor")
    case age >= 18 && age < 65:
        fmt.Println("Adult")
    default:
        fmt.Println("Senior")
    }
}

The switch evaluates conditions rather than values. Since 42 matches the second case, default doesn't execute here.

Default in type switches

Type switches can use default to handle unexpected types. This example demonstrates type checking with a fallback.

type_default.go
package main

import "fmt"

func checkType(x interface{}) {
    switch x.(type) {
    case int:
        fmt.Println("Integer")
    case string:
        fmt.Println("String")
    case bool:
        fmt.Println("Boolean")
    default:
        fmt.Println("Unknown type")
    }
}

func main() {
    checkType(42)
    checkType("hello")
    checkType(3.14)
}

The default case catches the float64 value (3.14) since it's not explicitly handled. It prints "Unknown type" for that input.

Default position matters

default can appear anywhere in a switch. This example shows how position affects execution flow.

position_default.go
package main

import "fmt"

func main() {
    value := 3
    
    switch value {
    default:
        fmt.Println("Default case first")
    case 1:
        fmt.Println("One")
    case 2:
        fmt.Println("Two")
    case 3:
        fmt.Println("Three")
    }
}

Despite default appearing first, Go still evaluates cases in order. The matching case (3) executes, showing position doesn't affect logic.

Default with empty cases

Empty cases fall through to default. This example demonstrates how empty cases interact with the default case.

empty_default.go
package main

import "fmt"

func main() {
    letter := 'b'
    
    switch letter {
    case 'a', 'e', 'i', 'o', 'u':
        fmt.Println("Vowel")
    case ' ':
        // Empty case falls through
    default:
        fmt.Println("Consonant or other")
    }
}

The space character matches an empty case, causing execution to fall through to default. The output would be "Consonant or other" for 'b'.

Practical example: Error handling

This practical example uses default for clean error handling in a function that processes different status codes.

error_default.go
package main

import "fmt"

func handleStatus(code int) string {
    switch code {
    case 200:
        return "OK"
    case 404:
        return "Not Found"
    case 500:
        return "Internal Server Error"
    default:
        return fmt.Sprintf("Unknown status: %d", code)
    }
}

func main() {
    fmt.Println(handleStatus(200))
    fmt.Println(handleStatus(403))
}

The default case gracefully handles unexpected status codes like 403, returning a descriptive message instead of failing.

Default vs if-else

This example contrasts default with traditional if-else chains to show when each approach is preferable.

default_vs_ifelse.go
package main

import "fmt"

func main() {
    // Switch with default
    fruit := "dragonfruit"
    switch fruit {
    case "apple", "pear":
        fmt.Println("Common fruit")
    case "durian":
        fmt.Println("Exotic fruit")
    default:
        fmt.Println("Unknown fruit")
    }
    
    // Equivalent if-else
    if fruit == "apple" || fruit == "pear" {
        fmt.Println("Common fruit")
    } else if fruit == "durian" {
        fmt.Println("Exotic fruit")
    } else {
        fmt.Println("Unknown fruit")
    }
}

Both approaches produce the same output. Switch with default is cleaner for multiple discrete values, while if-else better suits ranges.

Source

Go language specification

This tutorial covered the default keyword in Go with practical examples of switch statement fallbacks in various scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Golang tutorials.