Complete the code to get the length of the string in Go.
str := "hello" length := len([1]) fmt.Println(length)
string or len as argument instead of the variable.In Go, len() returns the number of bytes in the string. We pass the string variable str to get its length.
Complete the code to convert a string to a slice of runes for proper Unicode handling.
str := "hello" runes := [][1](str) fmt.Println(runes)
byte which only handles raw bytes, not Unicode.int or string which are incorrect types here.In Go, converting a string to a slice of rune allows correct handling of Unicode characters, as rune represents a Unicode code point.
Fix the error in the code to correctly print each Unicode character in the string.
str := "héllo" for _, [1] := range str { fmt.Printf("%c ", r) }
In a range loop over a string, the second variable holds the rune (Unicode character). The code prints r, so the variable must be named r.
Fill both blanks to correctly convert a string to a byte slice and back to string.
str := "hello" bytes := [][1](str) newStr := string([2]) fmt.Println(newStr)
rune instead of byte for byte slice.To convert a string to bytes, use []byte. To convert bytes back to string, use string(bytes).
Fill all three blanks to create a map of characters to their counts from a string.
str := "hello" counts := make(map[[1]]int) for _, [2] := range str { counts[[3]]++ } fmt.Println(counts)
byte as map key which may not handle Unicode correctly.The map key type should be rune to handle Unicode characters. The loop variable is commonly c, which is used as the key to count characters.
