Panic: runtime error: index out of range [5] with length 5 [closed]

0

I wrote this program,

package main

import "fmt"

func main() {
    x := "Hello"

    for i := 0; i <= 10; i++ {
        fmt.Printf("%#U\n", x[i])
    }
}

https://go.dev/play/p/yrMu2hlAvkZ

panic: runtime error: index out of range [5] with length 5

I know the reason why it's giving the error, it's due to the condition in for loop i<=10 and if I remove the = it will not give me the error.

But let's say I want to code it in such a way that I won't get errors if I still use i<=10. How can it be done?

for-loop go
2021-11-24 04:33:54
2

1

I want to code it in such a way that I won't get error if i still use "i<=10". How can it be done?

You can safely loop like this,

package main

import "fmt"

func main() {
    x := "Hello"
    for i := 0; i <= 10 && i < len(x); i++ {
        fmt.Printf("%#U\n", x[i])
    }
}

https://go.dev/play/p/2NknjS3Ql6k

Or this,

package main

import "fmt"

func main() {
    x := "Hello"
    for i := 0; i <= 10; i++ {
        fmt.Printf("%#U\n", x[i%len(x)])
    }
}

https://go.dev/play/p/0eKTcxXipwB

2021-11-24 06:11:58
0

You can either change your condition, from i <= 10 to

i <= 4 // 4 is the last index of your string

or you can increase your string length, from x := "Hello" to

x := "Hello World"
2021-11-24 05:52:29

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................