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

Constants और iota

एक constant एक labeled value है जो एक बार सेट होने के बाद हमेशा के लिए locked हो जाती है, और iota एक magic counter है जो Go आपको constants की list को मुफ़्त में number करने के लिए देता है।
Syntax
go
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

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

Grouped Constants

var की तरह, कई संबंधित constants को एक parenthesized const block में साथ declare किया जा सकता है, जो fixed values के एक group को organized और scan करने में आसान रखता है।

उदाहरण: Grouped Constants

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

Introducing iota

किसी const block के अंदर, identifier iota पहली line पर 0 से शुरू होता है और हर बाद की line के लिए 1 बढ़ता है, भले ही आप इसे हर line पर explicitly न लिखें।

यह आपको हर number हाथ से टाइप किए बिना संबंधित constant values की एक sequence generate करने का एक compact तरीका देता है।

Note: हर नए const( ) block की शुरुआत में iota 0 पर reset हो जाता है।

उदाहरण: Introducing iota

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

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

markup
package main

import "fmt"

const (
	_  = iota // skip 0
	KB = 1 << (10 * iota)
	MB
	GB
)

func main() {
	fmt.Println("KB:", KB, "MB:", MB, "GB:", GB)
}
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. किसी const को reassign करने की कोशिश करना, जो एक compile-time error है क्योंकि constants definition से immutable हैं।
  2. यह उम्मीद करते हुए iota उपयोग करना कि यह प्रति const block reset होता है जबकि यह असल में प्रति line/spec एक बार increment होता है, नामों के प्रति group नहीं।
  3. यह मान लेना कि 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:

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.