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)
}
}