Basic types in Golang

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

Variables

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

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128

string

變數宣告與印出

package main
import "fmt"

func main() {
  i := 100
  ui := uint(i)
  f := 1.425
  b := true
  s := "Hello World"
  c := 3 + 4i
  fmt.Printf("%T, %T,%T,%T,%T,%T", i, ui, f, b, s, c)
}

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