Skip to content
GwiyeomGo Tech Blog
About GwiyeomGo

Golang 의 switch 문을 써서 type 에 따라 코드 실행을 다르게 하자

GOLANG, 20231 min read

var content dtos.Test
if err := ctx.Bind(&content); err != nil {
return ctx.JSON(http.StatusBadRequest, err.Error())
}
switch content.Type {
case "A":
fmt.Println("a")
break
case "B:
fmt.Println("b")
fallthrough
default:
fmt.Println("c")
}

break

content 의 Type 이 A 인경우 a 출력 하고 switch 문을 나가고 다음 코드 실행

fallthrough

A fallthrough statement transfers control to the next case. It may be used only as the final statement in a clause.

content 의 Type 이 B 인경우 bc를 출력 하고 switch 문을 나가고 다음 코드 실행

참고

https://yourbasic.org/golang/switch-statement/

© 2024 by GwiyeomGo Tech Blog. All rights reserved.