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

os Package का उपयोग

os package Go का खुद operating system से connection है -- यह वह तरीका है जिससे कोई program command-line input पढ़ता है, environment settings देखता है, और files से बात करता है।
Syntax
go
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

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() {
	// Print the values followed by a newline
	fmt.Println("program name:", os.Args[0])
}

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 कर देता है।

Note: चूंकि os.Exit defers skip करता है, जब cleanup मायने रखे तो main() को normally return होने दें।

उदाहरण: Exiting with a Status Code

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() {
	// 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")
}

Getting the Current Working Directory

os.Getwd वह directory return करता है जहां से program वर्तमान में चल रहा है, साथ ही एक error अगर यह तय न हो पाए -- उन file paths बनाने के लिए उपयोगी जो program जहां launch हुआ उसके सापेक्ष हों।

उदाहरण: Getting the Current Working Directory

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 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))
}
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 functions से returned error जांचना भूल जाना, क्योंकि कई operations (file खोलना, एक variable पढ़ना) fail हो सकते हैं।
  2. os.Args[0] (program name) को पहले असली command-line argument के साथ confuse करना, जो असल में os.Args[1] है।
  3. 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:

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.