Go言語_rune
rune.go
package try
import "fmt"
func Run_Rune() {
Rune("Hello,世界!")
}
func Rune(s string) {
str := []rune(s) // str []rune
for i, r := range str {
fmt.Printf("i%d r %c\n", i, r)
}
fmt.Println("------")
for i, v := range s { // var v rune
fmt.Printf("i%d r %c\n", i, v)
}
}
実行結果
@Mac feb_twelve % go version
go version go1.14.1 darwin/amd64
@Mac feb_twelve % sw_vers
ProductName: macOS
ProductVersion: 12.1
BuildVersion: 21C52
@Mac feb_twelve % tree
.
├── go.mod
├── main.go
└── try
└── rune.go
@Mac feb_twelve % go run main.go
i0 r H
i1 r e
i2 r l
i3 r l
i4 r o
i5 r ,
i6 r 世
i7 r 界
i8 r !
------
i0 r H
i1 r e
i2 r l
i3 r l
i4 r o
i5 r ,
i6 r 世
i9 r 界
i12 r !
@Mac feb_twelve %
解説
.goenv/versions/1.14.1/src/builtin/builtin.go
// rune is an alias for int32
type rune = int32
To make things dependable, there are normalization techniques that guarantee that a given character is always represented by the same code points
~
“Code point” is a bit of a mouthful, so Go introduces a shorter term for the concept: rune
by The Go Blog
参考
Slice string into letters - stackoverflow
Strings, bytes, runes and characters in Go - The Go Blog