os Package का उपयोग
In this page:
f, err := os.Open("path")
os.Exit(code)
os.Getenv("NAME")
What the os Package Provides
os package underlying operating system का Go का gateway है: files पढ़ना और लिखना, environment variables inspect करना, command-line arguments access करना, और process exit codes control करना।
उदाहरण: What the os Package Provides
// 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() {
// Print the values followed by a newline
fmt.Println("program name:", os.Args[0])
}
Login to try C/C++/Java/PHP code in the editor
Exiting with a Status Code
os.Exit(code) program को दिए गए exit status के साथ तुरंत terminate करता है, program को launch करने वाली किसी भी चीज़ को success (0) या failure (non-zero) signal देने के लिए उपयोगी -- लेकिन एक normal return के विपरीत, यह किसी भी pending deferred calls को skip कर देता है।
उदाहरण: Exiting with a Status Code
// 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() {
// Print the values followed by a newline
fmt.Println("about to check a condition")
// Short declaration: create ok and infer the type
ok := true
// Check whether !ok
if !ok {
os.Exit(1)
}
// Print the values followed by a newline
fmt.Println("condition passed, continuing normally")
}
Login to try C/C++/Java/PHP code in the editor
Getting the Current Working Directory
os.Getwd वह directory return करता है जहां से program वर्तमान में चल रहा है, साथ ही एक error अगर यह तय न हो पाए -- उन file paths बनाने के लिए उपयोगी जो program जहां launch हुआ उसके सापेक्ष हों।
उदाहरण: Getting the Current Working Directory
// 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 dir, err and infer the type
dir, err := os.Getwd()
// Handle the error if one occurred
if err != nil {
// Print the values followed by a newline
fmt.Println("error:", err)
return
}
// Print the values followed by a newline
fmt.Println("running from a directory (path length):", len(dir))
}
Login to try C/C++/Java/PHP code in the editor
- ज़्यादातर os functions से returned error जांचना भूल जाना, क्योंकि कई operations (file खोलना, एक variable पढ़ना) fail हो सकते हैं।
- os.Args[0] (program name) को पहले असली command-line argument के साथ confuse करना, जो असल में os.Args[1] है।
- pending defers वाले code के अंदर os.Exit() call करना, जो उन्हें पूरी तरह skip कर देता है -- os.Exit deferred functions नहीं चलाता।
- os package OS-level features तक access प्रदान करता है: files, environment variables, command-line arguments, और process control।
- os.Args program के command-line arguments रखता है, os.Args[0] खुद program name होने के साथ।
- os.Exit(code) program को तुरंत terminate करता है, किसी भी deferred function calls को skip करते हुए।
- बाहरी दुनिया को छूने वाले ज़्यादातर os functions एक error return करते हैं जिसे हमेशा जांचना चाहिए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: