GO์–ธ์–ด

[GO] ์ œ์–ด๋ฌธ ๋ฐ ๋ฐ˜๋ณต๋ฌธ

HHRR 2023. 1. 16. 14:20

If๋ฌธ

  • if๋ฌธ (if, if-else, if-else-if ๋‹ค ๋˜‘๊ฐ™์Œ)
package main

import "fmt"

func main() {

	var a int = 20
	b := 20

	//์˜ˆ์ œ1
	if a >= 15 {
		fmt.Println("15์ด์ƒ")
	}

	if b >= 25 {
		fmt.Println("25์ด์ƒ")
	}


	if c := true; c {
		fmt.Println("True")
	}
    
	//์˜ˆ์ œ3
	if c := 40; c >= 35 {
		fmt.Println("35์ด์ƒ")
	}
	//c+=20 ์—๋Ÿฌ๋ฐœ์ƒ(์œ„์—์„œ c ์‚ฌ์šฉํ›„ ์†Œ๋ฉธ๋จ)

}

- ๋ฐ˜๋“œ์‹œ Boolean ๊ฒ€์‚ฌ(true, false) ~ 0๊ณผ 1๋กœ ์‚ฌ์šฉ ๋ชปํ•จ(์ž๋™ ํ˜•๋ณ€ํ™˜ ๋ถˆ๊ฐ€ํ•˜๊ธฐ ๋•Œ๋ฌธ)
- ์ฃผ์˜! if๋ฌธ ์•ˆ์—์„œ ์งง์€ ์„ ์–ธ์œผ๋กœ ๋ณ€์ˆ˜ ์„ ์–ธํ•˜์—ฌ ์‚ฌ์šฉ ํ›„ if๋ฌธ ๋ฐ–์—์„œ ์‚ฌ์šฉ ๋ชป ํ•จ

  • if๋ฌธ ์ฃผ์˜์ 
//์—๋Ÿฌ ๋ฐœ์ƒ1
	
		if b>= 25
		{

		}
//์—๋Ÿฌ ๋ฐœ์ƒ2
		if b>=25
			fmt.Println("25์ด์ƒ")
//์—๋Ÿฌ ๋ฐœ์ƒ3
			if c := 1; c{
			fmt.Println("True")
		}

- ๊ด„ํ˜ธ ๋ฐ˜๋“œ์‹œ if ์“ด ์ค„์—์„œ ์—ด์–ด์•ผํ•จ
- ๊ด„ํ˜ธ ์—†์œผ๋ฉด ์•ˆ ๋จ
- 0๊ณผ 1๋กœ T, F ๊ฒ€์‚ฌ ๋ชปํ•จ


switch๋ฌธ

  • switch๋ฌธ (๋งŽ์ด ์“ฐ์ž„โญ)
package main

import "fmt"

func main() {

	//์˜ˆ์ œ1
	a := -7
	switch {
	case a < 0:
		fmt.Println(a, "๋Š” ์Œ์ˆ˜")
	case a == 0:
		fmt.Println(a, "๋Š” 0")
	case a > 0:
		fmt.Println(a, "๋Š” ์–‘์ˆ˜")
	}

	//์˜ˆ์ œ2
	switch b := 27; {
	case b < 0:
		fmt.Println(b, "๋Š” ์Œ์ˆ˜")
	case b == 0:
		fmt.Println(b, "๋Š” 0")
	case b > 0:
		fmt.Println(b, "๋Š” ์–‘์ˆ˜")
	}

	//์˜ˆ์ œ3
	switch c := "go"; c { //c ์งง์€ ์„ ์–ธํ›„ c์‚ฌ์šฉ ํ•˜์—ฌ ๋งค์น˜
	case "go":
		fmt.Println("Go!")
	case "java":
		fmt.Println("Java!")
	default:
		fmt.Println("์ผ์น˜ ๊ฐ’ ์—†์Œ")
	}

	//์˜ˆ์ œ4
	switch c := "go"; c + "lang" {
	case "golang":
		fmt.Println("Golang")
	case "java":
		fmt.Println("Java!")
	default:
		fmt.Println("None!")
	}

	//์˜ˆ์ œ5 ๋น„๊ต
	switch i, j := 20, 30; {
	case i < j:
		fmt.Println("i๋Š” j๋ณด๋‹ค ์ ๋‹ค")
	case i == j:
		fmt.Println("i์™€ j๋Š” ๊ฐ™๋‹ค")
	case i > j:
		fmt.Println("i๋Š” j๋ณด๋‹ค ํฌ๋‹ค")

	}

}


- Switch ๋’ค ํ‘œํ˜„์‹(Expression) ์ƒ๋žต ๊ฐ€๋Šฅ, case ๋’ค ํ‘œํ˜„์‹(Expression) ์‚ฌ์šฉ ๊ฐ€๋Šฅ
- ์ž๋™ break ๋จ.
- Type ๋ถ„๊ธฐ -> ๊ฐ’์ด ์•„๋‹Œ ๋ณ€์ˆ˜ Type์œผ๋กœ ๋ถ„๊ธฐ ๊ฐ€๋Šฅ
- default : case ์ด์™ธ์˜ ๊ฒฝ์šฐ์— ์‹คํ–‰

  • fallthrough
package main

import (
	"fmt"
)

func main() {
	//์˜ˆ์ œ1
	a := 30 / 15
	switch a {
	case 2, 4, 6: //i๊ฐ€ 2, 4, 6์ธ ๊ฒฝ์šฐ
		fmt.Println("a -> ", a, "๋Š” ์ง์ˆ˜")
	case 1, 3, 5: //i๊ฐ€ 1, 3, 5์ธ ๊ฒฝ์šฐ
		fmt.Println("a -> ", a, "๋Š” ํ™€์ˆ˜")
	}

	//์˜ˆ์ œ2
	switch e := "go"; e {
	case "java":
		fmt.Println("Java!")
		fallthrough
	case "go":
		fmt.Println("go!") //์กฐ๊ฑด ๋งค์นญ ๋’ค ๋ฌธ์žฅ ๋น ์ ธ๋‚˜๊ฐ ์ž๋™ break
		fallthrough
	case "python":
		fmt.Println("python")
		fallthrough
	case "ruby":
		fmt.Println("ruby")
		fallthrough
	case "c":
		fmt.Println("c")
	}
}

- ์กฐ๊ฑด์— ๋งž๋Š” ๋ฌธ์žฅ ์•„๋ž˜์— ๋ฐ˜๋“œ์‹œ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ๋ฌธ์žฅ -> ํ•ด๋‹น ๋ฌธ์žฅ ์•„๋ž˜์— fallthrough ์ ์œผ๋ฉด ๋จ
- ์œ„์˜ ์˜ˆ์ œ์—์„œ case "go" ์ดํ›„์˜ case๋“ค ์ „๋ถ€ ์‹คํ–‰๋จ.


for๋ฌธ

  • for๋ฌธ (๋ฐ˜๋ณต๋ฌธ for๋ฌธ ๋ฐ–์— ์—†์Œ, ์ „์ฒ˜๋ฆฌ)
package main

import "fmt"

func main_for1() {

	//์˜ˆ์ œ1
	for i := 0; i < 5; i++ {
		fmt.Println("ex1 : ", i)
	}

	//์˜ˆ์ œ2 (๋ฌดํ•œ๋ฃจํ”„)
		for{
			fmt.Println("ex2 : Helllo, Golang!")
			fmt.Println("ex2 : Infinite loop!")
		}

	//์˜ˆ์ œ 3 (range์šฉ๋ฒ•) ๋ฐฐ์—ด
	loc := []string{"seoul", "busan", "incheon"} //๋ฐฐ์—ด
	for index, name := range loc {               //loc ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์™€ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜ด
		fmt.Println("ex3 : ", index, name)
	}

}


- go์—์„œ ์œ ์ผํ•˜๊ฒŒ ์ œ๊ณต๋˜๋Š” ๋ฐ˜๋ณต๋ฌธ(๋‹ค์–‘ํ•œ ์‚ฌ์šฉ๋ฒ• ์ˆ™์ง€)
- for ๋’ค์— ์กฐ๊ฑด ์•ˆ์จ์ฃผ๋ฉด ๋ฌดํ•œ๋ฃจํ”„
- ๊ด„ํ˜ธ ์œ„์น˜(for๋ฌธ ์ค„์—์„œ ๊ด„ํ˜ธ ์—ด๊ธฐ), ๊ด„ํ˜ธ ์œ ๋ฌด ์ฃผ์˜

  • for๋ฌธ 2
package main

import "fmt"

func main() {
	//์˜ˆ์ œ1
	sum1 := 0
	for i := 0; i <= 100; i++ {
		sum1 += i
	}
	fmt.Println("ex1 : ", sum1)

	//์˜ˆ์ œ2 (๋” ๋งŽ์ด์”€)
	sum2, i := 0, 0
	for i <= 100 { //i๋Š” 100์ดํ•˜ ์ˆ˜
		sum2 += i
		i++
		//j := i++ ->์—๋Ÿฌ (go์—์„œ ํ›„์น˜์—ฐ์‚ฐ์€ ๋ฐ˜ํ™˜๊ฐ’x)
	}
	fmt.Println("ex2 : ", sum2)

	//์˜ˆ์ œ3 while ํ˜•ํƒœ๋ž‘ ๋น„์Šท
	sum3, i := 0, 0
	for { 
		if i > 100 {
			break
		}
		sum3 += i
		i++
	}
	fmt.Println("ex3 : ", sum3)

	//์˜ˆ์ œ4
	for i, j := 0, 0; i <= 10; i, j = i+1, j+10 { //์งง์€์„ ์–ธ ํ•œ๋ฒˆ ์ดˆ๊ธฐํ™”ํ•˜๋ฉด ๋Œ€์ž…ํ•ด์•ผํ•จ. ๋‹ค์‹œ ์งง์€์„ ์–ธ x
		fmt.Println("ex4 : ", i, j) //i๋Š” 1์”ฉ ์ฆ๊ฐ€, j๋Š” 10์”ฉ ์ฆ๊ฐ€
	}

	//์—๋Ÿฌ ๋ฐœ์ƒ
	/*
		for i, j := 0, 0; i <= 10; i++, j+=10 {
		}
		//ํ›„์น˜์—ฐ์‚ฐ์€ ๋ฐ˜ํ™˜๊ฐ’ ์—†์Œ!
	*/

}

- ์˜ˆ์ œ1 == ์˜ˆ์ œ2 == ์˜ˆ์ œ3 -> 5050
- go ์–ธ์–ด ํ›„์น˜์—ฐ์‚ฐ์€ ๋ฐ˜ํ™˜๊ฐ’ ์—†์Œ

  • Loop ์‚ฌ์šฉ
package main

import "fmt"

func main() {
	//์˜ˆ์ œ1
Loop1:
	for i := 0; i < 5; i++ {
		for j := 0; j < 5; j++ {
			if i == 2 && j == 4 {
				break Loop1 //Loop1๋กœ ๊ฐ€(๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ)
			}
			fmt.Println("ex 1 : ", i, j)
		}
	}

	//์˜ˆ์ œ2
	for i := 0; i < 10; i++ {
		if i%2 == 0 {
			continue
		}
		fmt.Println("ex2 : ", i)
	}

Loop2:
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			if i == 1 && j == 2 {
				continue Loop2
			}
			fmt.Println("ex3 : ", i, j)
		}
	}

}

- Loop๋ฌธ ์•ˆ์—๋Š” continue, break ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด๋ฌธ์ด๋‚˜ ๋ฐ˜๋ณต๋ฌธ ์™€์•ผํ•จ


Go ๋ฌธ๋ฒ• ํŠน์ง• ์ •๋ฆฌ


1.
- Go : ๋ชจํ˜ธํ•˜๊ฑฐ๋‚˜ ์• ๋งคํ•œ ๋ฌธ๋ฒ• ๊ธˆ์ง€
- ํ›„์น˜ ์—ฐ์‚ฐ์ž ํ—ˆ์šฉ i++, ์ „์น˜ ์—ฐ์‚ฐ์ž ๋น„ํ—ˆ์šฉ ++i(x)
- ์ฆ๊ฐ์—ฐ์‚ฐ ๋ฐ˜ํ™˜๊ฐ’ ์—†์Œ sum := i++(x)
- ํฌ์ธํ„ฐ(Pointer ํ—ˆ์šฉ, ์—ฐ์‚ฐ ๋น„ํ—ˆ์šฉ)

2.
- ๋ฌธ์žฅ ๋ ์„ธ๋ฏธ์ฝœ๋ก (;) ์ฃผ์˜, ์ž๋™์œผ๋กœ ๋์— ์„ธ๋ฏธ์ฝœ๋ก  ์‚ฝ์ž…
- ๋‘ ๋ฌธ์žฅ์„ ํ•œ ๋ฌธ์žฅ์— ํ‘œํ˜„ํ•  ๊ฒฝ์šฐ ๋ช…์‹œ์ ์œผ๋กœ ์„ธ๋ฏธ์ฝœ๋ก  ์‚ฌ์šฉ(๊ถŒ์žฅํ•˜์ง€ ์•Š์Œ)
- ๋ฐ˜๋ณต๋ฌธ ๋ฐ ์ œ์–ด๋ฌธ(์กฐ๊ฑด๋ฌธ) (if, for)

3.
- ์ฝ”๋“œ ์„œ์‹ ์ง€์ • : ํ•œ ์‚ฌ๋žŒ์ด ์ฝ”๋”ฉ ํ•œ ๊ฒƒ ๊ฐ™์€ ์ผ๊ด€์„ฑ ์œ ์ง€
- ์ฝ”๋“œ ์Šคํƒ€์ผ ์œ ์ง€
- gofmt -h : ์‚ฌ์šฉ๋ฒ•
- gofmt -w : ์›๋ณธํŒŒ์ผ์— ๋ฐ˜์˜