Skip to content

Instantly share code, notes, and snippets.

@jcamilom
Created May 3, 2018 20:05
Show Gist options
  • Save jcamilom/ae08298c9a5d00e1eb2988da9ebcdecc to your computer and use it in GitHub Desktop.
Save jcamilom/ae08298c9a5d00e1eb2988da9ebcdecc to your computer and use it in GitHub Desktop.
[Accessing bytes and chars of a string] Getting the corresponding bytes and chars that compose a string. From https://golangbot.com/strings/ #go #golang #strings
package main
import (
"fmt"
)
func printBytes(s string) {
for i:= 0; i < len(s); i++ {
fmt.Printf("%x ", s[i])
}
}
func printChars(s string) {
for i:= 0; i < len(s); i++ {
fmt.Printf("%c ",s[i])
}
}
func main() {
name := "Hello World"
printBytes(name)
fmt.Printf("\n")
printChars(name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment