panic: runtime error: index out of range in Go

If the user does not provide any input, the inp array is empty. This means that even the index 0 is out of range, i.e. inp[0] can’t be accessed.

You can check the length of inp with len(inp) before checking inp[0] == "add". Something like this might do:

if len(inp) == 0 {
    fmt.Println("you didn't type add")
} else if inp[0] == "add" {
    fmt.Println("you typed add")
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)