To get a character at a specific index in a string in Go, you can use array-like indexing or the []
operator.
In this example,
str
with a value of "Hello World"
.[]
operator on the string str
.package main
import "fmt"
func main() {
str := "Hello World"
charAtIndex6 := str[6]
fmt.Println("Character at index 6:", string(charAtIndex6))
}
Character at index 6: W
In this example,
str
with a value of "PROGRAMGURU"
.[]
operator on the string str
.package main
import "fmt"
func main() {
str := "PROGRAMGURU"
charAtIndex2 := str[2]
fmt.Println("Character at index 2:", string(charAtIndex2))
}
Character at index 2: O
In this tutorial, we learned How to get Character at Specific Index in String in Go language with well detailed examples.