goto Statement क्या है
In this page:
goto Label
// ...
Label:
// statements
Basic goto Usage
एक goto statement control को उसी function में कहीं और एक label पर transfer करता है। label बस एक colon के बाद वाला identifier है, जिसे आप जिस statement पर jump करना चाहते हैं उससे पहले रखा जाता है।
उदाहरण: Basic goto Usage
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create i and infer the type
i := 0
Loop:
if i < 3 {
// Print the values followed by a newline
fmt.Println("i =", i)
i++
goto Loop
}
// Print the values followed by a newline
fmt.Println("done")
}
Login to try C/C++/Java/PHP code in the editor
Restrictions on goto
Go का compiler किसी goto को किसी block के बीच में या किसी ऐसी variable declaration को पार करके jump करने से मना करता है जो अभी execute नहीं हुई, जो C जैसी पुरानी languages में goto को बदनाम करने वाले कई classic bugs और undefined states को रोकता है।
उदाहरण: Restrictions on goto
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create n and infer the type
n := 5
// Check whether n > 0
if n > 0 {
goto Positive
}
// Print the values followed by a newline
fmt.Println("non-positive")
return
Positive:
fmt.Println("n is positive:", n)
}
Login to try C/C++/Java/PHP code in the editor
Breaking Out of Nested Loops
असली Go code में goto अब भी दिखाई देने वाली कुछ जगहों में से एक deeply nested loops से सीधे बाहर jump करना है जब एक labeled break अजीब होगा, हालांकि labeled break/continue (अलग से कवर किया गया) को आमतौर पर प्राथमिकता दी जाती है।
उदाहरण: Breaking Out of Nested Loops
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Loop: repeat while the condition holds
for i := 0; i < 3; i++ {
// Loop: repeat while the condition holds
for j := 0; j < 3; j++ {
// Check whether i == 1 && j == 1
if i == 1 && j == 1 {
goto Done
}
// Print the values followed by a newline
fmt.Println(i, j)
}
}
Done:
fmt.Println("exited nested loops")
}
Login to try C/C++/Java/PHP code in the editor
- goto का ज़्यादा उपयोग करके जटिल control flow बनाना जो एक loop या function के रूप में कहीं ज़्यादा साफ़ होता, 'spaghetti code' बनाते हुए जिसे follow करना मुश्किल है।
- किसी block के बीच में या किसी variable declaration के ऊपर jump करने की कोशिश करना, जिसे Go का compiler explicitly मना करता है।
- एक loop simulate करने के लिए goto उपयोग करना जब एक साधारण for loop (या label के साथ break/continue) पहले से ज़्यादा idiomatically यह काम करता है।
- 'goto label' execution को सीधे उसी function में एक labeled statement पर jump करता है।
- Go goto को सीमित करता है ताकि यह किसी block में jump न कर सके या किसी variable declaration को skip न कर सके, कई classic goto bugs को रोकते हुए।
- goto idiomatic Go में शायद ही कभी उपयोग होता है, ज़्यादातर deeply nested loops से बाहर निकलने या generated code के लिए रखा जाता है।
- Labeled break/continue आमतौर पर goto से कहीं ज़्यादा साफ़ तरीके से वही समस्या हल करते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: