Command-Line Arguments क्या हैं
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
// 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])
}
Login to try C/C++/Java/PHP code in the editor
Safely Accessing Arguments
चूंकि अगर user कोई argument भूल जाए तो os.Args उम्मीद से छोटा हो सकता है, code को runtime panic से बचने के लिए इसमें index करने से पहले इसकी length जांचनी चाहिए।
उदाहरण: Safely Accessing Arguments
// 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])
}
Login to try C/C++/Java/PHP code in the editor
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि os.Args[0] हमेशा program का अपना नाम है, पहला असली argument नहीं -- असली arguments index 1 से शुरू होते हैं।
- पहले len(os.Args) जांचे बिना os.Args में index करना, ज़रूरी argument गायब होने पर एक index-out-of-range panic का कारण बनते हुए।
- 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: