Environment Variables क्या हैं
In this page:
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Checking If a Variable Is Set
os.LookupEnv value और एक boolean दोनों return करता है जो बताता है कि variable असल में मौजूद था या नहीं, आपको एक जान-बूझकर खाली value को एक गायब वाली से अलग करने देते हुए।
उदाहरण: Checking If a Variable Is Set
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Setting a Default When Unset
एक आम pattern environment से एक configuration value पढ़ना और अगर यह सेट न हो तो एक sensible default पर गिरना है, optional settings के लिए program crash से बचते हुए।
उदाहरण: Setting a Default When Unset
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि os.Getenv किसी गायब variable के लिए एक error return करता है -- यह चुपचाप इसके बजाय एक खाली string return करता है।
- जब आपको किसी unset variable को जान-बूझकर खाली string पर सेट किए एक से अलग करना हो तब os.LookupEnv उपयोग न करना।
- 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: