← Back to Go Course | Chapter 13: File IO & OS | Lesson 6 of 7

Environment Variables क्या हैं

एक environment variable आपके program के बाहर, operating system के environment में stored एक setting है, जिसे आपका program बिना hardcode किए configuration जैसी चीज़ें जानने के लिए झांक सकता है।
Syntax
go
value := os.Getenv("NAME")
value, ok := os.LookupEnv("NAME")
os.Setenv("NAME", "value")

Reading an Environment Variable

os.Getenv किसी नामित environment variable की value निकालता है, अगर यह सेट नहीं है तो एक खाली string return करते हुए -- साधारण, लेकिन यह 'not set' को 'set to empty' से अलग नहीं कर सकता।

उदाहरण: Reading an Environment Variable

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"os"
)

// main is where the program starts running
func main() {
	os.Setenv("APP_MODE", "production")
	// Short declaration: create mode and infer the type
	mode := os.Getenv("APP_MODE")
	// Print the values followed by a newline
	fmt.Println("running in mode:", mode)
}

Checking If a Variable Is Set

os.LookupEnv value और एक boolean दोनों return करता है जो बताता है कि variable असल में मौजूद था या नहीं, आपको एक जान-बूझकर खाली value को एक गायब वाली से अलग करने देते हुए।

Note: जब भी एक खाली string unset से मायने रखने वाले तरीके से अलग case हो तो LookupEnv उपयोग करें।

उदाहरण: Checking If a Variable Is Set

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"os"
)

// main is where the program starts running
func main() {
	// Short declaration: create val, ok and infer the type
	val, ok := os.LookupEnv("MISSING_VAR")
	// Print the values followed by a newline
	fmt.Println("value:", val, "was set:", ok)
}

Setting a Default When Unset

एक आम pattern environment से एक configuration value पढ़ना और अगर यह सेट न हो तो एक sensible default पर गिरना है, optional settings के लिए program crash से बचते हुए।

उदाहरण: Setting a Default When Unset

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"os"
)

// Define the function getEnvOrDefault
func getEnvOrDefault(key, fallback string) string {
	// Check whether val, ok := os.LookupEnv(key); ok
	if val, ok := os.LookupEnv(key); ok {
		// Send val back to the caller
		return val
	}
	// Send fallback back to the caller
	return fallback
}

// main is where the program starts running
func main() {
	// Short declaration: create port and infer the type
	port := getEnvOrDefault("PORT", "8080")
	fmt.Println("using port:", port)
}
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. यह मान लेना कि os.Getenv किसी गायब variable के लिए एक error return करता है -- यह चुपचाप इसके बजाय एक खाली string return करता है।
  2. जब आपको किसी unset variable को जान-बूझकर खाली string पर सेट किए एक से अलग करना हो तब os.LookupEnv उपयोग न करना।
  3. sensitive values (जैसे API keys) को environment से पढ़ने के बजाय सीधे source code में hardcode करना।
चैप्टर सारांश
  • os.Getenv(name) एक environment variable पढ़ता है, अगर यह unset हो तो एक खाली string return करते हुए।
  • os.LookupEnv(name) अतिरिक्त रूप से एक boolean return करता है जो बताता है कि variable असल में सेट था या नहीं।
  • os.Setenv current process (और इसके children) के लिए एक environment variable सेट करता है।
  • Environment variables किसी program में बिना hardcode किए configuration पास करने का एक आम तरीका हैं।
🔒

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.