← Back to Go Course | Chapter 2: Variables & Types | Lesson 2 of 7

:= से Short Variable Declaration

:= shortcut आपको एक labeled box बनाने और उसमें एक साथ कुछ रखने देता है, बिना यह बताए कि वह किस तरह का box है।
Syntax
go
name := value
name1, name2 := value1, value2

The := Operator

किसी function के अंदर, ':=' एक नया variable declare करता है और दाईं ओर की value से इसका type infer करता है, बिना var keyword या किसी explicit type की ज़रूरत के।

यह 'var x = value' का shorthand है और यह local variables declare करने का Go code का कहीं ज़्यादा आम तरीका है।

Note: := Go में local variables declare करने का idiomatic तरीका है; var का सहारा मुख्यतः तब लें जब आपको एक explicit type या एक zero value चाहिए।

उदाहरण: The := Operator

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 name and infer the type
	name := "Priya"
	// Short declaration: create age and infer the type
	age := 28
	// Print the values followed by a newline
	fmt.Println(name, "is", age, "years old")
}

Multiple Assignment with :=

आप एक single line पर ':=' से एक साथ कई variables declare कर सकते हैं, नामों और values को commas से अलग करते हुए। यह उन functions के साथ खासतौर पर आम है जो कई values return करते हैं, जैसे map lookups या error-returning calls।

उदाहरण: Multiple Assignment with :=

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 x, y and infer the type
	x, y := 10, 20
	// Short declaration: create sum, diff and infer the type
	sum, diff := x+y, x-y
	// Print the values followed by a newline
	fmt.Println("sum:", sum, "diff:", diff)
}

At Least One New Variable Rule

Go ':=' को उसी scope में एक मौजूदा variable reuse करने देता है जब तक बाईं ओर कम से कम एक variable genuinely नया हो -- मौजूदा वाला सिर्फ reassign होता है, redeclare नहीं।

अगर हर नाम पहले से मौजूद हो, compiler इसे 'no new variables on left side of :=' के साथ reject कर देता है।

उदाहरण: At Least One New Variable Rule

markup
package main

import "fmt"

func main() {
	count, err := 5, error(nil)
	fmt.Println(count, err)

	count, ok := 10, true // count reused, ok is new
	fmt.Println(count, ok)
}

Shadowing in Nested Blocks

चूंकि ':=' एक नया variable declare कर सकता है, इसे if statement जैसे किसी nested block के अंदर उपयोग करना गलती से एक दूसरा, असंबंधित variable बना सकता है जो उसी नाम के साथ उस block के बाकी हिस्से के लिए outer वाले को shadow कर देता है।

यह beginners के लिए सूक्ष्म bugs का एक आम स्रोत है।

Note: अगर कोई variable किसी if/for block के अंदर अप्रत्याशित व्यवहार करे, जांचें कि क्या ':=' ने गलती से outer वाले को reuse करने के बजाय एक नया shadowed variable बनाया।

उदाहरण: Shadowing in Nested Blocks

markup
package main

import "fmt"

func main() {
	value := 1
	if true {
		value := 2 // shadows outer value inside this block
		fmt.Println("inner:", value)
	}
	fmt.Println("outer:", value)
}
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. किसी function body के बाहर (package level पर) := उपयोग करने की कोशिश करना, जहां सिर्फ var की अनुमति है।
  2. किसी उसी scope में पहले से मौजूद variable पर फिर से := उपयोग करना जब कोई नया variable introduce नहीं हो रहा, जिससे एक 'no new variables' compile error आता है।
  3. if/for block के अंदर := उपयोग करके गलती से किसी outer variable को shadow करना, outer वाले को reuse करने के बजाय एक नया local variable बनाते हुए।
चैप्टर सारांश
  • ':=' एक step में एक variable declare और initialize करता है, value से इसका type infer करते हुए।
  • यह सिर्फ function bodies के अंदर उपयोग किया जा सकता है, package level पर कभी नहीं।
  • ':=' के बाईं ओर कम से कम एक variable उस scope में नया होना चाहिए।
  • nested blocks में := का ज़्यादा उपयोग गलती से outer scope के variables को shadow कर सकता है।
🔒

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.