Back to all posts
programming golang go

Go Switch

0 min read (75 words)

I like switch in Go.

func main() {
    all := []string{"Denver", "London", "NY", "SF"}

    for _, city := range all {
        switch city {
        case "Denver":
            // code for Denver
            fmt.Println("Denver")
        case "London", "SF": // London OR SF the same logic
            // code for London/SF
            fmt.Println("London/SF")
        case "NY":
            // code for NY
            fmt.Println("NY")
        default:
            // code default
            fmt.Println("default")
        }
    }
}

Very easy to use, we just create cases and what to do by default:

Go code example demonstrating switch statement syntax with multiple cases
Terminal output showing the results of switch statement execution
Dmitry Golovach
About

Dmitry Golovach

Principal Network Engineer and AI enthusiast. Always learning, always building.

Share this post

All posts