Skip to content

Instantly share code, notes, and snippets.

@montanaflynn
Created November 16, 2014 09:07
Show Gist options
  • Save montanaflynn/b267a379b11c39cf7f64 to your computer and use it in GitHub Desktop.
Save montanaflynn/b267a379b11c39cf7f64 to your computer and use it in GitHub Desktop.
Go strings
package main
import "fmt"
func main() {
// Create a string
str := "Hello"
// Split it into unicode characters
chars := []rune(str)
// Pick out individual unicode code
firstchar := chars[0]
fmt.Println(firstchar)
// Turn it back into a string
firstcharstring := string(firstchar)
fmt.Println(firstcharstring)
// You don't have to use runes
name := "John"
fc := string(name[1])
fmt.Println(fc)
// But it only works for ASCII
fmt.Println(string("世界"[1]))
// Runes work with unicode chars
fmt.Println(string([]rune("世界")[1]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment