A byte takes 8 bits of storage. In Go, it is equivalent to the data type uint8. It is used to represent an ASCII character.
package mainimport "fmt"func main() {// Declaration and assignmentvar a1 byte = 97var a2 byte = 'b'// Printing without character formattingfmt.Println(a1)fmt.Println(a2)// Printing with character formattingfmt.Printf("%c\n", a1)fmt.Printf("%c\n", a2)}
fmt
.97
to a1
which is the ASCII equivalent of 'a'. b
to a2
.a1
and a2
without character formatting. The output without character formatting is the ASCII equivalent code of the characters.A rune takes 4 bytes or 32 bits for storage. It is an int32 equivalent. Runes are used to represent the Unicode characters, which is a broader set than ASCII characters. These Unicode characters are encoded in the UTF-8 format.
package mainimport ("fmt""reflect")func main() {// Shorthand declaration and assignmenta1 := 97a2 := 'b'a3 := '♬'// Printing the value, unicode equivalent and type of the variablefmt.Printf("Value: %c, Unicode: %U, Type: %s\n", a1, a1, reflect.TypeOf(a1))fmt.Printf("Value: %c, Unicode: %U, Type: %s\n", a2, a2, reflect.TypeOf(a2))fmt.Printf("Value: %c, Unicode: %U, Type: %s\n", a3, a3, reflect.TypeOf(a3))}
fmt
and reflect
.97
to a1
which is the ASCII equivalent of 'a'. b
to a2
.♬
to a3
.a1
, a2
and a3
.The type of a1
is int. This is because we had assigned an integer 97
to it. The type of both a2
and a3
is int32. This is because the default type of character value is a rune.
Byte | Rune |
It takes 8 bits to store a value. | It takes 32 bits to store a value. |
Byte is equivalent to uint8. | Rune is equivalent to int32. |
Declaration: var a2 byte = 'b' | Declaration: var a2 rune = 'b' |
It has to be declared explicitly. | It is the default type of a character. |
A string is a read-only slice of bytes effectively. When we iterate over a string using a for
loop, we iterate over a slice of bytes. However, each value is decoded as a rune when we use a for range
loop.
package mainimport ("fmt""reflect")func main() {s := "hello"// for loopfor i := 0; i < len(s); i++ {fmt.Printf("Value: %c, Unicode: %U, Type: %s\n", s[i], s[i], reflect.TypeOf(s[i]))}fmt.Println()// for rangefor _, value := range s {fmt.Printf("Value: %c, Unicode: %U, Type: %s\n", value, value, reflect.TypeOf(value))}}
fmt
and reflect
.s
and assign "hello"
to it.s
using a for
loop. Since it is printing one character at a time, the type of each element printed is uint8.s
using a for range
loop. Here the value is by default of type rune.The difference in the representation can be seen from the output of the execution of the above code.
A byte and a rune are both used to represent a character in Go. The default type for character value is a rune.