[Go] switch문

2022. 8. 16. 22:03·개발/Go
728x90
반응형

1. switch문 기본형태

switch 비굣값{
    case 값1:
    문장
    case 값2:
    문장
    default:
    문장
}

 

예제)

package main

import "fmt"

func main(){
	a:=3

	switch a {
	case 1:
		fmt.Println("a == 1")
	case 2: 
		fmt.Println("a ==2")
	case 3: 
		fmt.Println("a ==3")
	default:
		fmt.Println("a>3")
	}
}
//a ==3

switch문을 사용하는경우 : if문으로 썼을때 조건이 복잡해 보이는 경우 편하게 하기 위함

if문과 switch문중 더 편한걸 선택해서 사용하면 된다.

 

2. switch 초기문

switch도 if문과 같이 초기문을 써줄수 있고 쓰는 방식도 같다.

switch 초기문; 비굣값{
    case 값1:
    문장
    default:
    문장
}

 

예제)

package main

import "fmt"

func getMyAge() int{
	return 22
}

func main(){
	switch age:=getMyAge();age {
	case 10:
		fmt.Println("Teenage")
	case 33:
		fmt.Println("Pair3")
	}
}

3. 열거값

package main

import "fmt"

//타입을 새로 만들때 type
type ColorType int

const (
	Red ColorType = iota
	Blue
	Green
	Yellow
)

func colorToString(color ColorType) string{
	switch color{
	case Red:
		return "Red"
	case Blue:
		return "Blue"
	case Green:
		return "Green"
	case Yellow:
		return "Yellow"
	default:
		return "Undefined"
	}
}

func getMyFavoriteColor() ColorType{
	return Red
}

func main(){
	fmt.Println("My favorite color is", colorToString(getMyFavoriteColor()))
}

//My favorite color is Red

4. break, fallthrough

go는 다른언어와는 다르게 switch문 안에 break가 없어도 된다.

물론 break를 써줘도 된다.

break를 안써도 switch문을 빠져나오는데 바로 빠져나오는걸 방지하기 위해 fallthrough를 넣어주면 빠져나오지않고 다음조건으로 넘어가게 된다.

 

예제)

package main

import "fmt"

func main(){
	a:=3

	switch a {
	case 1:
		fmt.Println("a==1")
		break
	case 2:
		fmt.Println("a==2")
	case 3:
		fmt.Println("a==3")
		fallthrough
	case 4:
		fmt.Println("a==4")
	default:
		fmt.Println("a != 1,2,3")
	}
}
728x90
반응형

'개발 > Go' 카테고리의 다른 글

[Go] if 문  (0) 2022.08.15
[Go] const(상수)  (0) 2022.08.11
[Go] 함수(function)  (0) 2022.08.10
[Go] 연산자  (0) 2022.08.10
[Go] Golang fmt 패키지  (0) 2022.08.03
'개발/Go' 카테고리의 다른 글
  • [Go] if 문
  • [Go] const(상수)
  • [Go] 함수(function)
  • [Go] 연산자
TeTedo.
TeTedo.
  • TeTedo.
    TeTedo 개발 일기
    TeTedo.
  • 전체
    오늘
    어제
    • 분류 전체보기 (319)
      • 개발 (274)
        • Article (4)
        • 정리 (21)
        • Spring Boot (17)
        • JPA (2)
        • JAVA (6)
        • Database (4)
        • 자료구조 (11)
        • 알고리즘 (32)
        • React (20)
        • Docker (10)
        • node.js (18)
        • Devops (11)
        • Linux (4)
        • TypeScript (3)
        • Go (10)
        • HyperLedger (4)
        • BlockChain (43)
        • html, css, js (48)
        • CS (3)
        • AWS (3)
      • 모아두고 나중에 쓰기 (3)
      • 팀프로젝트 (18)
        • SNS(키보드워리어) (9)
        • close_sea (9)
      • 개인프로젝트 (1)
        • Around Flavor (1)
        • CHAM (13)
        • ethFruitShop (5)
      • 독서 (0)
        • 스프링부트와 AWS로 혼자 구현하는 웹 서비스 (0)
  • 블로그 메뉴

    • 홈
    • 개발일기
    • CS
    • 실습
    • 코딩테스트
    • 웹
    • Go
    • node.js
    • 팀플
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    nodejs
    node.js
    React
    CSS
    블록체인
    30일챌린지
    go
    30일 챌린지
    하이퍼레저
    ERC721
    go언어
    html
    프로그래머스
    erc20
    node
    컨테이너
    js
    명령어
    mysql
    도커
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
TeTedo.
[Go] switch문
상단으로

티스토리툴바