main Package Import ( "FMT" ) FUNC main () { // string underlayer is a byte array, thus slicing processing may be performed string str: = "hello @ atguigu" //: = type inference // microtome acquired atguigu Slice: STR = [. 6:] fmt.Println ( "Slice =", Slice) // string is immutable, it can not be said str [0] = 'z' way to modify the string // str [0 ] = 'z' [compiled by not being given, because the string is an immutable] // To change the string, first string -> [] byte / or [] Rune -> modify -> rewrite turn into String // "Hello @ atguigu" => change "zello @ atguigu" of arr1: = [] byte (STR) of arr1 [0] = 'Z' STR = String (of arr1) fmt.Println ( "STR =",str) // details, we turn to [] after byte, can handle English and numbers, but can not handle Chinese // The reason is [] byte byte to deal with, but a Chinese character is three bytes, so there will be garbled // The solution is transformed into the string [] Rune to as [] Rune is processed by character, kanji compatible arr2: = [] rune (str ) arr2 [0] = 'North' str2 = string (arr2 is) fmt.Println ( "STR =", str2 ) }
Go slices and string
Guess you like
Origin www.cnblogs.com/yzg-14/p/12229975.html
Recommended
Ranking