Get the first rune of a string in Go

This example program illustrates how to get the first rune from a string.

package main

import (
        "fmt"
        "unicode/utf8"
)

func main() {
        string := "あいうえお";
        runeValue, _ := utf8.DecodeRuneInString(string)
        fmt.Printf("Your rune %c has value %X\n", runeValue, runeValue)
}

(download)

The output looks like this:

Your rune あ has value 3042


Copyright © Ben Bullock 2009-2024. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer