Basic types in Golang

了解 Golang 有哪些資料型態. 官網上的 Basic types 列出了多種的型態.

Variables

下面列出資料類型, 其中可分為 BooleansNumeric TypesComplex NumbersStrings

 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

typesize十進制範圍
int依據系統依據系統
int88 bits-128 ~ 127
int1616 bits-32768 ~ 32767
int3232 bits-2147483648 ~ 2147483647
int6464 bits-9223372036854775808 ~ 9223372036854775807

Unsigned Integers

typesize十進制範圍
uint依據系統依據系統
uint88 bits0 ~ 255
uint1616 bits0 ~ 65535
uint3232 bits0 ~ 4294967295
uint6464 bits0 ~ 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.

typesize
float3232 bits or 4 bytes
float6464 bits or 8 bytes

Complex Numbers

typeproperty
complex64實數與虛數都是 float32
complex128實數與虛數都是 float64

References

comments powered by Disqus