Go - type switch and type assertions

00_8_3·2023년 5월 9일
0

Go

목록 보기
10/10

타입 어설션

실제의 타입을 다른 데이터 타입으로 변환하지 않는다.

컴파일 타임에만 적용. 런타임에는 변환하지 않는다.

타입 스위치와 타입 어설션

v가 어떠한 타입인지 확인하기 위해 switch가 분기 처리를 하여
맞는 타입을 실행한다.

var x interface{} = "foo"

switch v := x.(type) {
case nil:
	fmt.Println("x is nil")            // here v has type interface{}
case int: 
	fmt.Println("x is", v)             // here v has type int
case bool, string:
	fmt.Println("x is bool or string") // here v has type interface{}
default:
	fmt.Println("type unknown")        // here v has type interface{}
}

x.type 의 type은 25개 예약어 키워드 중 하나이다.

0개의 댓글