In Go, you can use array-like indexing to access characters in a string.
In this example,
str
with a value of "Hello World"
.str
.package main
import "fmt"
func main() {
str := "Hello World"
firstChar := str[0]
fmt.Printf("First character: %c\n", firstChar)
}
First character: H
In this example,
str
with a value of "apple"
.str
.package main
import "fmt"
func main() {
str := "apple"
firstChar := str[0]
fmt.Printf("First character: %c\n", firstChar)
}
First character: a
In this tutorial, we learned How to get First Character in String in Go language with well detailed examples.