Sunday, March 26, 2023
HomeGolangSort of string letter complication - Obtaining Assistance

Sort of string letter complication – Obtaining Assistance


Why a solitary letter in go is a string kind:

 myLetter:= "S".
fmt.Println( reflect.TypeOf( myLetter)).
//.
// string.

… however letter drawn from the string as an table component is uint8:

 myString:= "String".
myLetter:= myString[0]
fmt.Println( reflect.TypeOf( myLetter)).
//.
// uint8.

… and also when I utilize for-loop its int32 every one of the unexpected:

 myString:= "String".
for _, v:= variety myString {
fmt.Println( reflect.TypeOf( v)).
//.
// int32 (x5).
}

This is soooo complicated me when I code, plus I require to create added transforming features whenever I do something string-manipulation-related.

What is the thinking behind this? Do you have any kind of methods to enhance my operations?


Hi @maykie,

Whatever that is bordered by dual quotes is an actual string, despite how much time that string is.

A specific component of a string, accessed as myString[0] is constantly a byte kind, which is an pen names for uint8 There is no char key in Go; usage byte rather.

To repeat over the bytes of a string, utilize a timeless for loophole: for i:= 0; i < < len( str); i++ {fmt.Print( str[i])}

The variety driver is Unicode-aware. Unlike a timeless loophole, an array loophole repeats over the private Unicode runes of a string. Runes are of kind rune, which is a pen name for int32


( Side note: reflect.TypeOf() has no suggestion concerning kind pen names.
As well as a pointer: fmt.Printf("% Tn", v) does the like fmt.Println( reflect.TypeOf( v)) however is much shorter, and also you do not need to import show right into your code.)



1 Like

RELATED ARTICLES

Most Popular

Recent Comments