Start learning free

LESSON 62 OF 671 · STRINGS I

Exceeding String Bounds in Go

Lesson

String indexes must stay inside the valid range.

For a string with length len(message), the first index is 0 and the last valid index is len(message) - 1.

message := "I love pancakes"

fmt.Println(string(message[len(message)-1])) // last character

Trying to access message[len(message)] goes past the end: the program panics with index out of range [15] with length 15.

Assignment
Follow the Coding Tutorial and print the last character without exceeding the string bounds.


STEP 1 OF 1
    Ctrl + Enter to check