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

var से Variables Declare करना

एक variable एक labeled box है जिसमें आप एक value रख सकते हैं, और var वह word है जो Go उस box को बनाने के लिए उपयोग करता है।
Syntax
go
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

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() {
	// 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)
}

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

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() {
	// 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)
}

Declaring Multiple Variables

एक single var block parentheses उपयोग करके एक साथ कई variables declare कर सकता है, जो संबंधित declarations को grouped और readable रखता है, खासकर package-level variables के लिए file के ऊपर।

Note: var block में संबंधित variables को group करना package-level declarations को organized रखने का एक आम तरीका है।

उदाहरण: Declaring Multiple Variables

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

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

markup
// 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)
}
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. एक var declare करना और कभी उपयोग न करना -- Go एक unused local variable वाले function को compile करने से मना करता है।
  2. यह भूल जाना कि बिना initial value के declared var को दूसरी languages में nil/undefined के बजाय अपने type की zero value मिलती है।
  3. जब 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:

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.