Go 언어 Switch 문 간결 설명

Beginner

This tutorial is from open-source community. Access the source code

소개 (Introduction)

switch 문은 표현식의 값에 따라 다른 코드 블록을 실행할 수 있게 해주는 조건문입니다. 이는 코드를 단순화하고 가독성을 높이는 강력한 도구입니다.

Switch (스위치)

이 랩에서는 입력 값에 따라 해당 메시지를 출력하기 위해 switch 문을 완성해야 합니다.

  • 문제를 해결하기 위해 switch 문을 사용해야 합니다.
  • 예상치 못한 입력 값을 처리하기 위해 default 케이스를 사용해야 합니다.
$ go run switch.go
Write 2 as two
It's a weekday
It's after noon
I'm a bool
I'm an int
Don't know type string

전체 코드는 다음과 같습니다.

// _Switch statements_ express conditionals across many
// branches.

package main

import (
	"fmt"
	"time"
)

func main() {

	// Here's a basic `switch`.
	i := 2
	fmt.Print("Write ", i, " as ")
	switch i {
	case 1:
		fmt.Println("one")
	case 2:
		fmt.Println("two")
	case 3:
		fmt.Println("three")
	}

	// You can use commas to separate multiple expressions
	// in the same `case` statement. We use the optional
	// `default` case in this example as well.
	switch time.Now().Weekday() {
	case time.Saturday, time.Sunday:
		fmt.Println("It's the weekend")
	default:
		fmt.Println("It's a weekday")
	}

	// `switch` without an expression is an alternate way
	// to express if/else logic. Here we also show how the
	// `case` expressions can be non-constants.
	t := time.Now()
	switch {
	case t.Hour() < 12:
		fmt.Println("It's before noon")
	default:
		fmt.Println("It's after noon")
	}

	// A type `switch` compares types instead of values.  You
	// can use this to discover the type of an interface
	// value.  In this example, the variable `t` will have the
	// type corresponding to its clause.
	whatAmI := func(i interface{}) {
		switch t := i.(type) {
		case bool:
			fmt.Println("I'm a bool")
		case int:
			fmt.Println("I'm an int")
		default:
			fmt.Printf("Don't know type %T\n", t)
		}
	}
	whatAmI(true)
	whatAmI(1)
	whatAmI("hey")
}

요약 (Summary)

이 랩에서는 표현식의 값에 따라 다른 코드 블록을 실행하기 위해 switch 문을 사용하는 방법을 배웠습니다. 또한 default 케이스를 사용하여 예상치 못한 입력 값을 처리하는 방법도 배웠습니다.