The post Character in Go (Golang) appeared first on Welcome To Golang By Example.
]]>Golang does not have any data type of ‘char‘. Therefore
To declare either a byte or a rune we use single quotes. While declaring byte we have to specify the type, If we don’t specify the type, then the default type is meant as a rune.
To declare a string, we use double quotes or backquotes. Double quotes string honors escape character while back quotes string is a raw literal string and doesn’t honor any kind of escaping.
See the program below. It shows
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
//If you don't specify type here
var b byte = 'a'
fmt.Println("Priting Byte:")
//Print Size, Type and Character
fmt.Printf("Size: %d\nType: %s\nCharacter: %c\n", unsafe.Sizeof(b), reflect.TypeOf(b), b)
r := '£'
fmt.Println("\nPriting Rune:")
//Print Size, Type, CodePoint and Character
fmt.Printf("Size: %d\nType: %s\nUnicode CodePoint: %U\nCharacter: %c\n", unsafe.Sizeof(r), reflect.TypeOf(r), r, r)
s := "µ" //Micro sign
fmt.Println("\nPriting String:")
fmt.Printf("Size: %d\nType: %s\nCharacter: %s\n", unsafe.Sizeof(s), reflect.TypeOf(s), s)
}
Output:
Priting Byte:
Size: 1
Type: uint8
Character: a
Priting Rune:
Size: 4
Type: int32
Unicode CodePoint: U+00A3
Character: £
Priting String:
Size: 16
Type: string
Character: µ
constant 285 overflows byte
invalid character literal (more than one character)
The post Character in Go (Golang) appeared first on Welcome To Golang By Example.
]]>