var से Variables Declare करना
In this page:
var name type
var name type = value
var name = value
Basic var Declaration
var keyword एक explicit नाम और type के साथ एक variable declare करता है: 'var age int'। जब तक आप इसे एक value assign नहीं करते, यह उस type की zero value रखता है -- numbers के लिए 0, strings के लिए "", booleans के लिए false।
यह कई languages से अलग है जहां एक uninitialized variable undefined या एक error होता है।
उदाहरण: Basic var Declaration
// 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() {
// Declare age with the zero value of its type
var age int
// Declare name with the zero value of its type
var name string
// Print the values followed by a newline
fmt.Println("age:", age)
// Print the values followed by a newline
fmt.Println("name:", name)
}
Login to try C/C++/Java/PHP code in the editor
Declaring and Initializing Together
आप var declaration की उसी line में एक initial value दे सकते हैं: 'var age int = 30'। अगर एक initializer मौजूद हो, Go अक्सर type infer कर सकता है, आपको explicit type पूरी तरह छोड़ने और 'var age = 30' लिखने देते हुए।
उदाहरण: Declaring and Initializing Together
// 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() {
// Declare int and set it to 30
var age int = 30
var city = "Bengaluru"
// Print the values followed by a newline
fmt.Println(age, city)
}
Login to try C/C++/Java/PHP code in the editor
Declaring Multiple Variables
एक single var block parentheses उपयोग करके एक साथ कई variables declare कर सकता है, जो संबंधित declarations को grouped और readable रखता है, खासकर package-level variables के लिए file के ऊपर।
उदाहरण: Declaring Multiple Variables
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
var (
appName = "Inventory"
appVersion = "1.0"
maxItems = 100
)
// main is where the program starts running
func main() {
// Print the values followed by a newline
fmt.Println(appName, appVersion, maxItems)
}
Login to try C/C++/Java/PHP code in the editor
Package-Level vs Local Variables
किसी function के अंदर var से declared variables उस function तक local होते हैं, जबकि किसी file के top level पर, किसी function के बाहर var declarations package-level variables बन जाते हैं जो पूरे package में दिखाई देते हैं।
Package-level var उन कुछ जगहों में से एक है जहां Go किसी variable को तुरंत उपयोग हुए बिना मौजूद रहने देता है।
उदाहरण: Package-Level vs Local Variables
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Declare counter with the zero value of its type
var counter int
// Define the function increment
func increment() {
counter++
}
// main is where the program starts running
func main() {
increment()
increment()
// Print the values followed by a newline
fmt.Println("counter:", counter)
}
Login to try C/C++/Java/PHP code in the editor
- एक var declare करना और कभी उपयोग न करना -- Go एक unused local variable वाले function को compile करने से मना करता है।
- यह भूल जाना कि बिना initial value के declared var को दूसरी languages में nil/undefined के बजाय अपने type की zero value मिलती है।
- जब initializer पहले से स्पष्ट कर दे तब भी अनावश्यक रूप से type लिखना, जब := या type inference साफ़ होता।
- 'var name type' एक explicit type वाला variable declare करता है।
- 'var name type = value' एक step में declare और initialize करता है।
- बिना value के declared variables को अपने आप उस type की zero value मिलती है।
- var एक parenthesized block के अंदर एक साथ कई variables declare कर सकता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: