Start learning free

LESSON 58 OF 671 · STRINGS I

Accessing Characters in Go

Lesson

We can access positions inside a Go string with square brackets.

Indexes start at 0, so the first byte in "Avocado" is at index 0:

fruit := "Avocado"
fmt.Println(string(fruit[0])) // Output: A

Index 6 is the last position in "Avocado":

fmt.Println(string(fruit[6])) // Output: o

Go note: fruit[0] gives a byte, so we wrap it with string(...) when we want to print it as a one-character string.

Assignment
Follow the Coding Tutorial and print the first and last characters of fruit.


STEP 1 OF 2
    Ctrl + Enter to check