In JavaScript, the charCodeAt() method returns the numeric Unicode value of a character within a string. To achieve the same functionality in Go, utilize the following steps:
Here's an example code snippet:
<code class="go">package main import "fmt" func main() { str := "s" // Convert string to a slice of runes runes := []rune(str) // Get the Unicode value of the first character unicodeValue := runes[0] // Print the Unicode value fmt.Println(unicodeValue) // Print the character fmt.Printf("%c", unicodeValue) }</code>
This code will print the Unicode value (115) and the character 's'.
Additional Note:
If you don't need the character representation, you can directly access the int32 representing the Unicode value of the rune by using string[index]. This is because runes are aliases for int32 in Go.
The above is the detailed content of How do I get the Unicode value of a character in Go?. For more information, please follow other related articles on the PHP Chinese website!