Constants और iota
In this page:
const name = value
const (
A = iota
B
C
)
Declaring Constants
const keyword एक ऐसी value declare करता है जो compile time पर fixed है और कभी reassign नहीं हो सकती।
Constants mathematical constants, configuration limits, या ऐसे labels जैसी values के लिए उपयोगी हैं जो program के execution के दौरान गलती से कभी नहीं बदलने चाहिए।
उदाहरण: Declaring Constants
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
const MaxRetries = 3
// main is where the program starts running
func main() {
// Print the values followed by a newline
fmt.Println("Max retries allowed:", MaxRetries)
}
Login to try C/C++/Java/PHP code in the editor
Grouped Constants
var की तरह, कई संबंधित constants को एक parenthesized const block में साथ declare किया जा सकता है, जो fixed values के एक group को organized और scan करने में आसान रखता है।
उदाहरण: Grouped Constants
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
const (
StatusOK = 200
StatusNotFound = 404
StatusError = 500
)
// main is where the program starts running
func main() {
// Print the values followed by a newline
fmt.Println(StatusOK, StatusNotFound, StatusError)
}
Login to try C/C++/Java/PHP code in the editor
Introducing iota
किसी const block के अंदर, identifier iota पहली line पर 0 से शुरू होता है और हर बाद की line के लिए 1 बढ़ता है, भले ही आप इसे हर line पर explicitly न लिखें।
यह आपको हर number हाथ से टाइप किए बिना संबंधित constant values की एक sequence generate करने का एक compact तरीका देता है।
उदाहरण: Introducing iota
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
const (
Sunday = iota
Monday
Tuesday
Wednesday
)
// main is where the program starts running
func main() {
// Print the values followed by a newline
fmt.Println(Sunday, Monday, Tuesday, Wednesday)
}
Login to try C/C++/Java/PHP code in the editor
Using iota for Enumerated Values
iota को अक्सर साधारण expressions के साथ combine किया जाता है meaningful enumerations बनाने के लिए, जैसे byte-size units या status categories, जहां exact numeric value उतना मायने नहीं रखता जितना हर constant का अलग और ordered होना।
उदाहरण: Using iota for Enumerated Values
package main
import "fmt"
const (
_ = iota // skip 0
KB = 1 << (10 * iota)
MB
GB
)
func main() {
fmt.Println("KB:", KB, "MB:", MB, "GB:", GB)
}
Login to try C/C++/Java/PHP code in the editor
- किसी const को reassign करने की कोशिश करना, जो एक compile-time error है क्योंकि constants definition से immutable हैं।
- यह उम्मीद करते हुए iota उपयोग करना कि यह प्रति const block reset होता है जबकि यह असल में प्रति line/spec एक बार increment होता है, नामों के प्रति group नहीं।
- यह मान लेना कि const सिर्फ runtime पर ज्ञात values (जैसे किसी function call का result) के लिए काम करता है -- const को एक compile-time constant expression चाहिए।
- const एक ऐसी value declare करता है जो compilation के बाद नहीं बदल सकती।
- Constants को एक compile-time constant expression assign करना चाहिए, runtime computation नहीं।
- iota एक खास identifier है जो किसी const block के अंदर 0 से शुरू होकर अपने आप increment होता है।
- iota आमतौर पर readable enumerated constants बनाने के लिए उपयोग होता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: