| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
// Examples from the language spec section on string conversions. |
| 8 |
|
| 9 |
package main |
| 10 |
|
| 11 |
func main() { |
| 12 |
// 1 |
| 13 |
_ = string('a') // "a" |
| 14 |
_ = string(-1) // "\ufffd" == "\xef\xbf\xbd" |
| 15 |
_ = string(0xf8) // "\u00f8" == "ø" == "\xc3\xb8" |
| 16 |
|
| 17 |
type myString string |
| 18 |
_ = myString(0x65e5) // "\u65e5" == "日" == "\xe6\x97\xa5" |
| 19 |
|
| 20 |
// 2 |
| 21 |
_ = string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø" |
| 22 |
_ = string([]byte{}) // "" |
| 23 |
_ = string([]byte(nil)) // "" |
| 24 |
|
| 25 |
type bytes []byte |
| 26 |
_ = string(bytes{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø" |
| 27 |
|
| 28 |
type myByte byte |
| 29 |
_ = string([]myByte{'w', 'o', 'r', 'l', 'd', '!'}) // "world!" |
| 30 |
_ = myString([]myByte{'\xf0', '\x9f', '\x8c', '\x8d'}) // "🌍 |
| 31 |
|
| 32 |
// 3 |
| 33 |
_ = string([]rune{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔" |
| 34 |
_ = string([]rune{}) // "" |
| 35 |
_ = string([]rune(nil)) // "" |
| 36 |
|
| 37 |
type runes []rune |
| 38 |
_ = string(runes{0x767d, 0x9d6c, 0x7fd4}) // "\u767d\u9d6c\u7fd4" == "白鵬翔" |
| 39 |
|
| 40 |
type myRune rune |
| 41 |
_ = string([]myRune{0x266b, 0x266c}) // "\u266b\u266c" == "♫♬" |
| 42 |
_ = myString([]myRune{0x1f30e}) // "\U0001f30e" == "🌎 |
| 43 |
|
| 44 |
// 4 |
| 45 |
_ = []byte("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'} |
| 46 |
_ = []byte("") // []byte{} |
| 47 |
|
| 48 |
_ = bytes("hellø") // []byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'} |
| 49 |
|
| 50 |
_ = []myByte("world!") // []myByte{'w', 'o', 'r', 'l', 'd', '!'} |
| 51 |
_ = []myByte(myString("🌏")) // []myByte{'\xf0', '\x9f', '\x8c', '\x8f'} |
| 52 |
|
| 53 |
// 5 |
| 54 |
_ = []rune(myString("白鵬翔")) // []rune{0x767d, 0x9d6c, 0x7fd4} |
| 55 |
_ = []rune("") // []rune{} |
| 56 |
|
| 57 |
_ = runes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4} |
| 58 |
|
| 59 |
_ = []myRune("♫♬") // []myRune{0x266b, 0x266c} |
| 60 |
_ = []myRune(myString("🌐")) // []myRune{0x1f310} |
| 61 |
} |