(I'm using Go 1.14.6.)
The following statements would all output the char a
Println(string(int(97) ) )
Println(string(int32(97) ) )
Println(string([]int32{97} ) )
But
Println(string([]int{97} ) )
would cause compile error
cannot convert []int literal (type []int) to type string
The behavior is confusing to me. If it handles string(int) the same as string(int32), why it handles string([]int) different from string([]int32)?
runewhich represents a unicode code point is an alias forint32. So effectivelystring([]int32{})is the same asstring([]rune{})which converts a slice of runes (something like the charaters of astring) tostring. This is useful.intis notint32norrune, so it's not logical what converting[]inttostringshould be, it's ambiguous, so it's not allowed by the language spec.Converting an integer number to
stringresults in a string value with a singlerune. Spec: Conversions:This is confusing to many, as many expects the conversion result to be the (decimal) representation as string. The Go authors have recognized this, and have taken steps to depcecate and remove it from the language in the future. In Go 1.15,
go vetalready warns for such conversion. Go 1.15 release notes: Vet: