← Back to Go Course | Chapter 3: Control Flow | Lesson 5 of 7

goto Statement क्या है

goto आपके program को code में किसी labeled जगह पर सीधे jump करने देता है, जैसे order में पढ़ने के बजाय bookmarked page पर आगे skip करना।
Syntax
go
goto Label
// ...
Label:
    // statements

Basic goto Usage

एक goto statement control को उसी function में कहीं और एक label पर transfer करता है। label बस एक colon के बाद वाला identifier है, जिसे आप जिस statement पर jump करना चाहते हैं उससे पहले रखा जाता है।

उदाहरण: Basic goto Usage

markup
// 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")
}

Restrictions on goto

Go का compiler किसी goto को किसी block के बीच में या किसी ऐसी variable declaration को पार करके jump करने से मना करता है जो अभी execute नहीं हुई, जो C जैसी पुरानी languages में goto को बदनाम करने वाले कई classic bugs और undefined states को रोकता है।

उदाहरण: Restrictions on goto

markup
// 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)
}

Breaking Out of Nested Loops

असली Go code में goto अब भी दिखाई देने वाली कुछ जगहों में से एक deeply nested loops से सीधे बाहर jump करना है जब एक labeled break अजीब होगा, हालांकि labeled break/continue (अलग से कवर किया गया) को आमतौर पर प्राथमिकता दी जाती है।

Note: इसी मकसद के लिए एक labeled break आमतौर पर goto से ज़्यादा साफ़ है।

उदाहरण: Breaking Out of Nested Loops

markup
// 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")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. goto का ज़्यादा उपयोग करके जटिल control flow बनाना जो एक loop या function के रूप में कहीं ज़्यादा साफ़ होता, 'spaghetti code' बनाते हुए जिसे follow करना मुश्किल है।
  2. किसी block के बीच में या किसी variable declaration के ऊपर jump करने की कोशिश करना, जिसे Go का compiler explicitly मना करता है।
  3. एक 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.