I am writing a CLI interface program in Go.my program requires a user to enter a filename as an argument . following is the code I wrote to handle the situations in which user doesn't enter any argument . but it panics and gives an error "index out of range".how do I handle this?
package main
import (
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"os"
)
func main() {
if len(os.Args) == 0 {
fmt.Println("usage: gohex <filename>")
os.Exit(1)
} else {
filename := os.Args[1]
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
fmt.Println(hex.Dump(data))
}
}