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

Command-Line Arguments क्या हैं

Command-line arguments किसी program के नाम के बाद आप जो अतिरिक्त instructions टाइप करते हैं, जैसे किसी delivery driver को भेजते ही उसे एक address बताना।
Syntax
go
args := os.Args  // args[0] is the program

name := flag.String("name", "default", "usage")
flag.Parse()

Reading os.Args

os.Args strings की एक slice है जो program शुरू करने वाले command को रखती है: index 0 पर इसका अपना नाम, उसके बाद user द्वारा इसके बाद टाइप किया हर argument।

उदाहरण: Reading os.Args

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

Safely Accessing Arguments

चूंकि अगर user कोई argument भूल जाए तो os.Args उम्मीद से छोटा हो सकता है, code को runtime panic से बचने के लिए इसमें index करने से पहले इसकी length जांचनी चाहिए।

Note: position 0 से आगे index करने से पहले हमेशा len(os.Args) जांचें।

उदाहरण: Safely Accessing Arguments

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() {
	// Check whether len(os.Args) < 2
	if len(os.Args) < 2 {
		// Print the values followed by a newline
		fmt.Println("no extra arguments provided")
		return
	}
	// Print the values followed by a newline
	fmt.Println("first argument:", os.Args[1])
}

Parsing Flags with the flag Package

structured, named command-line options (raw positional arguments के बजाय) के लिए, standard library का flag package -name=value जैसी चीज़ों को अपने आप parse करता है, type conversion और default values सहित।

उदाहरण: Parsing Flags with the flag Package

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

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

// main is where the program starts running
func main() {
	// Short declaration: create name and infer the type
	name := flag.String("name", "World", "name to greet")
	flag.Parse()
	// Print using a format string
	fmt.Printf("Hello, %s!\n", *name)
}
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.Args[0] हमेशा program का अपना नाम है, पहला असली argument नहीं -- असली arguments index 1 से शुरू होते हैं।
  2. पहले len(os.Args) जांचे बिना os.Args में index करना, ज़रूरी argument गायब होने पर एक index-out-of-range panic का कारण बनते हुए।
  3. complex flag handling के लिए standard library के flag package उपयोग करने के बजाय manual os.Args parsing का सहारा लेना।
चैप्टर सारांश
  • os.Args एक []string है जो program name और इसे पास किए हर command-line argument को रखता है।
  • असली arguments index 1 से शुरू होते हैं; os.Args[0] हमेशा executable का अपना नाम/path है।
  • panic से बचने के लिए index करने से पहले हमेशा len(os.Args) जांचें, जब ज़रूरी arguments गायब हों।
  • flag package --verbose या -port=8080 जैसे named flags के लिए structured parsing प्रदान करता है।
🔒

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.