How to index characters in a Golang string?

How to get an “E” output rather than 69?

package main

import "fmt"

func main() {

Does [Golang] have function to convert a char to byte and vice versa?


which wM product do you refer to?
Please provide product name and version.


You can index characters by treating the string as a slice of bytes. Here is how you can do it.

package main

import (

func main() {
str := “Hello, World!”

// Accessing individual characters by index
// Note that Go uses zero-based indexing
charAtIndex := str[0] // Accessing the first character 'H'

// Iterating through the string and printing each character
for i := 0; i < len(str); i++ {
	fmt.Printf("Character at index %d: %c\n", i, str[i])


  1. We define a string str containing the text “Hello, World!”.
  2. To access an individual character at a specific index, you can use square brackets ([]) and provide the index you want to access. Keep in mind that Go uses zero-based indexing, so str[0] accesses the first character in the string.
  3. We then demonstrate how to iterate through the string using a for loop and print each character along with its index. We use len(str) to get the length of the string, which allows us to iterate over all its characters.

Remember that in Go, strings are immutable, so you cannot modify the characters directly by indexing. If you need to modify a string, you should convert it to a []rune (a slice of Unicode code points), make the modifications, and then convert it back to a string.

I hope it is useful. Please let me know, if you need further clarity.