Post

07. if문

07. if문

✅ 1. if문

1
2
3
4
5
if light == "green" {
	fmt.Println("길을 건넌다")
} else {
	fmt.Println("대기한다")
}

✅ 2. 쇼트서킷

  • && 연산은 좌변이 false라면 우변을 검사하지 않고 false로 처리한다
  • || 연산은 좌변이 true라면 우변을 검사하지 않고 true로 처리한다
  • 위와 같은 상황을 쇼트서킷(short-circuit)이라고 한다

✅ 3. if 초기문; 조건문

1
2
3
4
5
6
7
if filename, success := UploadFile(); success {
	fmt.Println("Upload success", filename) 
} else {
	fmt.Println("Failed to upload")
}

fmt.Println("filename is", filename) // Error - filename은 사라짐
  • if문 조건을 검사하기 전에 초기문을 통해 검사에 사용할 변수를 초기화할 수 있다
  • 초기문에서 선언한 변수의 범위는 if문 안으로 한정된다
This post is licensed under CC BY 4.0 by the author.