Confused about golang byte characters and code points

hello everyone
i need help regarding the following code snippet
var char byte
var codepoint rune
char = ‘é’
codepoint=‘é’
fmt.Println(string(char) , string(codepoint))

this code prints out ‘é’ two times , when I am expecting it to throw an error , because I am trying to assign a multibyte character to a byte type “char” , but it is working fine.
is my mental model wrong or what ?
whats the catch here ?

Because é seems to have the ordinal 233 which fits a byte without problems.

The “long s” (ſ) will cause a compilation error as its ordinal 383 doesn’t fit the byte:

1 Like

thanks for the reply mate but what I am not understanding is this that why is it 233
e.g
see the following code snippet
// Online Go compiler to run Golang program online
// Print “Try programiz.pro” message

package main
import “fmt”

func main() {
str:=“héllo”
fmt.Println(byte(str))
//outputs : [104 195 169 108 108 111]
var char byte
char=‘é’
fmt.Println(char)
//outputs : 233
}
in the first output the letter ’ é’ gets converted to two bytes “195 and 169” but in second instance it gets converted to 233 why is that ?

string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text. A string may be empty, but not nil. Values of string type are immutable.

U+00E9
195 169 is utf-8 (need 2 byte)(C3A9)
233 is raw utf-8 (need 1 int32) (E9)

1 Like

@danish: A leading bit of 1 in UTF-8 means that there is a following byte, so any code point greater than 0x7f must be encoded as two (or more) bytes. UTF-8 - Wikipedia

1 Like