Created
November 16, 2014 09:07
-
-
Save montanaflynn/b267a379b11c39cf7f64 to your computer and use it in GitHub Desktop.
Go strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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