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.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
