Go goto

Jmnote (토론 | 기여)님의 2023년 5월 4일 (목) 00:58 판
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

Go goto
Go goto 문
Go goto statements
  • goto는 지정한 레이블로 곧바로 넘어가도록 하는 키워드이다.
  • 레이블은 영문으로 시작해야 하며 숫자를 포함할 수 있다.
  • 제약은 없지만 첫자는 대문자로 쓰는 것이 관례인 것으로 보인다. (대략 PascalCase?)
  • goto 문은 일반적으로 사용이 권장되지 않는다. 꼭 필요한 경우가 아니라면, 이것 대신 for, if, switch, func로 구현하는 것이 좋다.
  • 암튼 있으니까 놀아보자. ㅎ
package main
import "fmt"
func main() {
    goto world
    fmt.Println("hello")
world:
    fmt.Println("world")
}
package main
import "fmt"
func main() {
    sum := 0
    i := 1
myloop:
    if i > 10 {
        goto mydone
    }
    sum += i
    i++
    goto myloop
mydone:
    fmt.Println(sum)
}
package main

import "fmt"

func main() {
	fmt.Println(numberInfo(0)) // hello
	fmt.Println(numberInfo(1)) // hello
	fmt.Println(numberInfo(2)) // world
	fmt.Println(numberInfo(3)) // lorem
	fmt.Println(numberInfo(4)) // hello
}

func numberInfo(num int) string {
	switch num {
	case 1:
		goto ONE
	case 2:
		goto Two
	case 3:
		goto three
	}
ONE:
	return "hello"
Two:
	return "world"
three:
	return "lorem"
}

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}