As I already mentioned in the previous posting, Go is a statically typed language.
So, the type of value is important.
You can get the type of value.
fmt.Println(reflect.TypeOf('a')) // int32
fmt.Println(reflect.TypeOf("Hello")) // string
fmt.Println(reflect.TypeOf(true)) // bool
fmt.Println(reflect.TypeOf(3)) // int
fmt.Println(reflect.TypeOf(3.14)) // float64
Rune
Rune represents a single character.
Rune literal uses a single quote.
Go uses a standard unicode when storing rune, and rune stores a number code, not a character itself.
Rune can use escape sequences.
Rune is the same as int32.
var x rune = 'a'
var y rune = '\t'
String
String is a series of bytes that represent text characters.
String literal uses a double quote.
String can use escape sequences.
The default value of string type is ""(empty string).
var x string = "Hello, Go!"
var y string = "Hi!\nNice to meet you."
Boolean
Boolean can take true or false.
The default value of boolean type is false.
var x bool = true
var y bool = false
Number
Go handles integer numbers and float point numbers as different types.
It has many types as per size and method.
e.g., uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32, float64, complex64, complex128, byte(same as uint8), int(depend on host), uint(depend on host)
The default value of number type is 0 or 0.0.
In addition, the default number type when you omit the type is int or float64.
var x byte = 7
var y uint32 = 1357
var z float32 = 3.14
Type Conversion
Arithmetic and comparison operations of Go can only use values of the same type.
width := 3
height := 5.2
area := width * height // Error: mismatched type
comparison := width > height // Error: mismatched type
If you really want to do this, you can use type conversion.
width := 3
height := 5.2
area := float64(width) * height
comparison := float64(width) > height
Other Types
Go has array, slice, structure, pointer, function, interface, map, and channel.
'Go' 카테고리의 다른 글
[Go] Configure VSCode (0) | 2020.11.28 |
---|---|
[Go] User-Defined Type (0) | 2020.11.02 |
[Go] Basic (0) | 2020.10.26 |
[Go] Variable (0) | 2020.10.21 |
[Go] Getting Started (시작하기) (0) | 2020.08.28 |
댓글