I'm writing a go program and I need to use command arguments. However if I don't pass arguments when I run the executable or go run gosite.go
the code it does the following runtime error.
panic: runtime error: index out of range
goroutine 1 [running]:
runtime.panic(0x80c8540, 0x816d4b7)
/usr/lib/go/src/pkg/runtime/panic.c:266 +0xac
main.main()
/home/jacob/github/gosite/src/github.com/zachdyer/gosite/gosite.go:11 +0x168
The error is found on line 11. So my question is am I using the os.Args
in the wrong way? Does this need to be initialized in a different way? Also why does it seem to be going in an infinite loop there? If I pass in an argument the program runs without any errors and prints the argument.
import (
"fmt"
"os"
)
var root string
func main() {
command := os.Args[1]
if command != "" {
fmt.Println(command)
} else {
command = ""
fmt.Println("No command given")
}
createDir("public")
createDir("themes")
}
func createDir(dir string) {
root = "../../../../"
err := os.Mkdir(root + dir, 0777)
if err != nil {
fmt.Println(err)
}
}
First check the length of the os.Args
slice and only index up to its length - 1:
if len(os.Args) > 1 {
command := os.Args[1]
// do something with command
} else {
// No arguments were specified!
}
os.Args
hold the command-line arguments, starting with the program name.
os.Args[0]
is the program name. If there are arguments, they go to Args[1]
, Args[2]
...
If no arguments were specified, the length of os.Args
will be 1 and will hold only the program name.
Also for easier and more sophisticated arguments handling check out the flag
package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With