:= से Short Variable Declaration
In this page:
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 का कहीं ज़्यादा आम तरीका है।
उदाहरण: The := Operator
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Multiple Assignment with :=
आप एक single line पर ':=' से एक साथ कई variables declare कर सकते हैं, नामों और values को commas से अलग करते हुए। यह उन functions के साथ खासतौर पर आम है जो कई values return करते हैं, जैसे map lookups या error-returning calls।
उदाहरण: Multiple Assignment with :=
// 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)
}
Login to try C/C++/Java/PHP code in the editor
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
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)
}
Login to try C/C++/Java/PHP code in the editor
Shadowing in Nested Blocks
चूंकि ':=' एक नया variable declare कर सकता है, इसे if statement जैसे किसी nested block के अंदर उपयोग करना गलती से एक दूसरा, असंबंधित variable बना सकता है जो उसी नाम के साथ उस block के बाकी हिस्से के लिए outer वाले को shadow कर देता है।
यह beginners के लिए सूक्ष्म bugs का एक आम स्रोत है।
उदाहरण: Shadowing in Nested Blocks
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)
}
Login to try C/C++/Java/PHP code in the editor
- किसी function body के बाहर (package level पर) := उपयोग करने की कोशिश करना, जहां सिर्फ var की अनुमति है।
- किसी उसी scope में पहले से मौजूद variable पर फिर से := उपयोग करना जब कोई नया variable introduce नहीं हो रहा, जिससे एक 'no new variables' compile error आता है।
- 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: