Lessons
Go Introduction
Go Get Started
Go Syntax
Go Comments
Go Variables
- Declare Variables
- Go में Variables का परिचय
- Go में Multiple Variable Declaration
- Variable Naming Rules (नामकरण के नियम)
Go Constants
Go Output
Go Data Types
- Basic Data Types
- Go Boolean Data Type (बूलियन डेटा प्रकार)
- Go Integer Data Types
- Go Float Data Types
- Go String Data Type
Go Arrays
Go Slices
Go Operators
- Go Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Go Conditions
Go Switch
Go Loops
Go Functions
Go Struct
Go Maps
Project Structure
Go में Multiple Variable Declaration
Go में Multiple Variable Declaration
Go भाषा में आप एक ही लाइन में कई variables घोषित कर सकते हैं। इससे कोड छोटा और पढ़ने में आसान हो जाता है।
उदाहरण 1: एक ही प्रकार (Same Type) के कई Variables
इस उदाहरण में चार variables (a, b, c, d) को एक साथ घोषित किया गया है, जिनका प्रकार int है।
package main
import ("fmt")
func main() {
var a, b, c, d int = 1, 3, 5, 7
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
}
type लिखते हैं, तो उस लाइन में सभी variables का प्रकार एक ही होना चाहिए।
उदाहरण 2: अलग-अलग प्रकार (Different Types) के Variables
यदि आप type नहीं लिखते हैं, तो आप एक ही लाइन में अलग-अलग प्रकार के variables घोषित कर सकते हैं।
package main
import ("fmt")
func main() {
var a, b = 6, "Hello"
c, d := 7, "World!"
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
}
समझाइए: ऊपर के कोड में, Go स्वयं मान से variable का प्रकार निर्धारित कर लेता है।
Go में Variable Declaration Block
Go में आप कई variable declarations को एक साथ block के रूप में समूहित (group) कर सकते हैं, जिससे कोड और अधिक पढ़ने योग्य बनता है।
package main
import ("fmt")
func main() {
var (
a int
b int = 1
c string = "hello"
)
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
}
समझाइए: ऊपर के block में तीन variables घोषित किए गए हैं। यह तरीका तब उपयोगी होता है जब हमें कई variables एक साथ परिभाषित करने हों।