Skip to content

Commit 7750fb7

Browse files
committed
Add string to rune slice example
1 parent 981f1c3 commit 7750fb7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package string_
2+
3+
import "fmt"
4+
5+
// StringToRuneSliceは、文字列とルーンスライスの遷移を表示するサンプルです
6+
func StringToRuneSlice() error {
7+
8+
// Go の 文字列 は、他の言語と同様に immutable となっている
9+
// なので、一度作成した文字列を変更することは出来ない
10+
// 変更したい場合は、新たな文字列を作って格納する必要がある
11+
s := "hello world"
12+
13+
// 文字列を runeスライス に変換
14+
r := []rune(s)
15+
16+
// 変更
17+
r[0] = 'H'
18+
19+
// 再度文字列へ
20+
s2 := string(r)
21+
22+
fmt.Printf("Before:\t%q\tAfter:\t%q\tRune:%v\n", s, s2, r)
23+
24+
return nil
25+
}

lib/mapping.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (m SampleMapping) MakeMapping() {
7373
m["comment01"] = comments.Comment01
7474
m["closure01"] = closure.Closure01
7575
m["string_rune_rawstring"] = string_.StringRuneRawString
76+
m["string_to_runeslice"] = string_.StringToRuneSlice
7677
m["set01"] = sets.Set01
7778
m["set02"] = sets.Set02
7879
m["set03"] = sets.Set03

0 commit comments

Comments
 (0)