Basic types in Golang
了解 Golang 有哪些資料型態. 官網上的 Basic types 列出了多種的型態.
Variables
下面列出資料類型, 其中可分為 Booleans
、Numeric Types
、Complex Numbers
、Strings
1bool
2
3string
4
5int int8 int16 int32 int64
6uint uint8 uint16 uint32 uint64 uintptr
7
8byte // alias for uint8
9
10rune // alias for int32
11 // represents a Unicode code point
12
13float32 float64
14
15complex64 complex128
16
17string
變數宣告與印出
1package main
2import "fmt"
3
4func main() {
5 i := 100
6 ui := uint(i)
7 f := 1.425
8 b := true
9 s := "Hello World"
10 c := 3 + 4i
11 fmt.Printf("%T, %T,%T,%T,%T,%T", i, ui, f, b, s, c)
12}
Booleans
Booleans have two possible values true
and fales
.
Numeric Types
Signed Integers
type | size | 十進制範圍 |
---|---|---|
int | 依據系統 | 依據系統 |
int8 | 8 bits | -128 ~ 127 |
int16 | 16 bits | -32768 ~ 32767 |
int32 | 32 bits | -2147483648 ~ 2147483647 |
int64 | 64 bits | -9223372036854775808 ~ 9223372036854775807 |
Unsigned Integers
type | size | 十進制範圍 |
---|---|---|
uint | 依據系統 | 依據系統 |
uint8 | 8 bits | 0 ~ 255 |
uint16 | 16 bits | 0 ~ 65535 |
uint32 | 32 bits | 0 ~ 4294967295 |
uint64 | 64 bits | 0 ~ 18446744073709551615 |
Float
float64 is the default float type. When you initialize a variable with a decimal value and don’t specify the float type, the default type inferred will be float64.
type | size |
---|---|
float32 | 32 bits or 4 bytes |
float64 | 64 bits or 8 bytes |
Complex Numbers
type | property |
---|---|
complex64 | 實數與虛數都是 float32 |
complex128 | 實數與虛數都是 float64 |