๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Data Base/Go

[Golang] Switch

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 11. 1.
๋ฐ˜์‘ํ˜•

Go์˜ switch๋Š” ๋’ค์ด์–ด ์˜ค๋Š” ๋ชจ๋“  case๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ,

์˜ค์ง ์ฒซ ๋ฒˆ์งธ๋กœ ์„ ํƒ๋œ ์ผ€์ด์Šค๋งŒ์„ ์‹คํ–‰ํ•œ๋‹ค๋Š” ์ ์„ ์ œ์™ธํ•˜๊ณ ๋Š” C๋‚˜ C++, Java, Javascript, PHP์™€ ์œ ์‚ฌํ•˜๋‹ค.

์‚ฌ์‹ค์ƒ ์œ„์˜ ์–ธ์–ด๋“ค์—์„œ๋Š” ๊ฐ๊ฐ์˜ case์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— break ๊ตฌ๋ฌธ์ด ํ•„์š”ํ•˜์ง€๋งŒ Go์—์„œ๋Š” ์ž๋™์œผ๋กœ break๊ฐ€ ์ œ๊ณต๋œ๋‹ค.

๋‹ค๋ฅธ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์€ Go์˜ switch case๋Š” ์ƒ์ˆ˜์ผ ํ•„์š”๊ฐ€ ์—†์œผ๋ฉฐ, ๊ทธ ๊ฐ’๋“ค์€ ์ •์ˆ˜์ผ ํ•„์š”๋„ ์—†๋‹ค.

 

 

์˜ˆ์ œ

package main

import (
	"fmt"
	"runtime"
)

func main() {
	fmt.Print("Go runs on ")
	switch os := runtime.GOOS; os {
	case "darwin":
		fmt.Println("OS X.")
	case "linux":
		fmt.Println("Linux.")
	default:
		// freebsd, openbsd,
		// plan9, windows...
		fmt.Printf("%s.\n", os)
	}
}

runtime.GOOS; ๋Š” runtime ํŒจํ‚ค์ง€์— ์ •์˜๋˜์–ด ์žˆ๋Š” ์ƒ์ˆ˜์ด๊ณ  ์šด์˜์ฒด์ œ๋ฅผ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

์šด์˜์ฒด์ œ์— ๋”ฐ๋ผ darwin, freebsd, linux, windows๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์ด ๊ฐ’์œผ๋กœ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

 

 

 

 

[์ฐธ๊ณ  ์‚ฌ์ดํŠธ - a Tour of Go]

๋ฐ˜์‘ํ˜•

'Data Base > Go' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Golang] Slices (์Šฌ๋ผ์ด์Šค)  (0) 2021.11.01
[Golang] defer  (0) 2021.11.01
[Golang] if๋ฌธ  (0) 2021.11.01
[Golang] C์˜ while = Go์˜ for  (0) 2021.11.01
[Golang] for ๋ฐ˜๋ณต๋ฌธ  (0) 2021.11.01

๋Œ“๊ธ€