Goland 1.整型数组与字符串的相互转化

1.1 byte[] 转 string

func bytesToString (bytes []byte) string{
	return string(bytes)
}
func main() {
	bytes := []byte{97, 98, 99}
	s := bytesToString(bytes)
	fmt.Println(s)
        // 打印 abc
}

1.2 string 转 byte[]

func stringToBytes (s string) []byte{
	return []byte(s)
}
func main() {
	s := "abc"
	bytes := stringToBytes(s)
	fmt.Println(bytes)
        // 打印 [97 98 99]
}

1.3 rune[] 转 string

func runesToString (runes []rune) string{
	return string(runes)
}
func main() {
	runes := []rune{33821, 33673, 33804}
	s := runesToString(runes)
	fmt.Println(s)
        // 打印 萝莉萌
}

1.4 string 转 rune[]

func stringToRunes (s string) []rune{
	return []rune(s)
}
func main() {
	s := "萝莉萌"
	runes := stringToRunes(s)
	fmt.Println(runes)
        // 打印 [33821 33673 33804]
}

你可能感兴趣的:(Goland)