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 Constants (स्थिरांक)
Go Constants (स्थायी मान)
यदि किसी वेरिएबल का मान स्थायी होना चाहिए और इसे बदला नहीं जा सकता, तो हम const कीवर्ड का उपयोग करते हैं।
Const कीवर्ड क्या है?
Const कीवर्ड वेरिएबल को "स्थायी" घोषित करता है, जिसका अर्थ है कि यह केवल पढ़ने योग्य (read-only) है और इसे बाद में बदला नहीं जा सकता।
सिंटैक्स
const CONSTNAME type = value
नोट: Constant का मान उसे घोषित करते समय ही दिया जाना चाहिए।
Constant घोषित करना
Example:
package main
import ("fmt")
const PI = 3.14
func main() {
fmt.Println(PI)
}
Constant के नियम
- Constant के नाम वेरिएबल की तरह ही नियमों का पालन करते हैं।
- Constant के नाम सामान्यतः बड़े अक्षरों में लिखे जाते हैं।
- Constants को किसी फंक्शन के अंदर या बाहर दोनों जगह घोषित किया जा सकता है।
Constant के प्रकार
Constants के दो प्रकार होते हैं:
- Typed Constants (टाइप वाला)
- Untyped Constants (बिना टाइप वाला)
Typed Constants
Typed constants को एक निश्चित प्रकार के साथ घोषित किया जाता है।
package main
import ("fmt")
const A int = 1
func main() {
fmt.Println(A)
}
Untyped Constants
Untyped constants को बिना किसी प्रकार के घोषित किया जाता है।
package main
import ("fmt")
const A = 1
func main() {
fmt.Println(A)
}
नोट: इस मामले में compiler खुद प्रकार तय करता है।
Constants: अपरिवर्तनीय और केवल पढ़ने योग्य
एक बार constant घोषित करने के बाद इसे बदलना संभव नहीं है:
package main
import ("fmt")
func main() {
const A = 1
A = 2
fmt.Println(A)
}
Result:
./prog.go:8:7: cannot assign to A
Multiple Constants Declaration
कई constants को एक ब्लॉक में घोषित किया जा सकता है:
package main
import ("fmt")
const (
A int = 1
B = 3.14
C = "Hi!"
)
func main() {
fmt.Println(A)
fmt.Println(B)
fmt.Println(C)
}