"Go goto"의 두 판 사이의 차이

 
(같은 사용자의 중간 판 4개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;Go goto
;Go goto
* 꼭 필요한 경우가 아니라면, 이것 대신 for, if, switch, func로 구현하는 것이 좋다.
;Go goto 문
;Go goto statements
* goto는 지정한 레이블로 곧바로 넘어가도록 하는 키워드이다.
* 레이블은 영문으로 시작해야 하며 숫자를 포함할 수 있다.
* 제약은 없지만 첫자는 대문자로 쓰는 것이 관례인 것으로 보인다. (대략 [[PascalCase]]?)
* goto 문은 일반적으로 사용이 권장되지 않는다. 꼭 필요한 경우가 아니라면, 이것 대신 for, if, switch, func로 구현하는 것이 좋다.
* 암튼 있으니까 놀아보자. ㅎ
* 암튼 있으니까 놀아보자. ㅎ


9번째 줄: 14번째 줄:
func main() {
func main() {
     goto world
     goto world
hello:
     fmt.Println("hello")
     fmt.Println("hello")
world:
world:
30번째 줄: 34번째 줄:
mydone:
mydone:
     fmt.Println(sum)
     fmt.Println(sum)
}
</syntaxhighlight>
<syntaxhighlight lang='go' run>
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"
}
}
</syntaxhighlight>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[Go switch]]
{{z컬럼3|
* [[goto]]
* [[Go 키워드]]
* [[Go 종료 문]]
* [[Go break 문]]
* [[Go switch ]]
* [[goto ]]
}}


==참고==
==참고==
* https://golang.org/src/math/gamma.go
* https://go.dev/ref/spec#Goto_statements
* https://pyrasis.com/book/GoForTheReallyImpatient/Unit18
* https://www.tutorialspoint.com/go/go_goto_statement.htm


[[분류: Go]]
[[분류: Go]]

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 }}