본문 바로가기
GO lang

조건문 if

by NOMADFISH 2020. 12. 18.

조건문

  • 보통 알고 있는 if문의 문법과 거의 동일하다.
    • if 조건 { } 의 형식이다.
    • if else 형식도 기존의 다른 언어 들과 동일하다.
    • 다만 조건문의 Scope는 python과는 다르게 Brace로 구별한다.
    • else if 문도 지원한다.
    • c++, java, javascript라 다른 점은 조건을 쓸때 괄호로 감싸지 않는다.
package main

import (
	"fmt"
	"math/rand"
)

func main() {

	randNumber := rand.Intn(20)
	if randNumber > 10 {
		fmt.Println("this number over 10")
	} else if randNumber > 5 {
		fmt.Println("this number over 5")
	} else {
		fmt.Println("this number under 5")
	}
}

 

짧은 조건문(inline code 첨가 가능)

  • 아래 볼 수 있는 것과 같이 if문 시작에 for문과 같이 짧은 코드를 넣을 수 있다. 보통은 변수 할당 등이 되겟다.
func main() {

	if randNumber := rand.Intn(20); randNumber > 10 {
		fmt.Println("this number over 10")
	} else if randNumber > 5 {
		fmt.Println("this number over 5")
	} else {
		fmt.Println("this number under 5")
	}
}

 

'GO lang' 카테고리의 다른 글

Switch 2  (0) 2020.12.26
Switch  (0) 2020.12.23
반복문  (0) 2020.12.14
Go lang #2 변수들  (0) 2020.12.09
GO lang 설치와 실행  (0) 2020.12.02