【Golang】数据类型

Posted by 西维蜀黍 on 2020-03-15, Last Modified on 2021-09-21

类型

序号 类型 描述
1 布尔型(boolean type) 布尔型的值只可以是常量 true 或者 false。一个简单的例子:var b bool = true。
2 数字类型(numeric type) 整型(integer values) int 和浮点型(floating-point values) float32、float64,Go 语言支持整型和浮点型数字,并且支持复数,其中位的运算采用补码。
3 字符串类型(string type) 字符串就是一串固定长度的字符连接起来的字符序列。Go 的字符串是由单个字节连接起来的。Go 语言的字符串的字节使用 UTF-8 编码标识 Unicode 文本。
4 派生类型: 包括:(a) 指针类型(Pointer)(b) 数组类型(c) 结构化类型(struct)(d) Channel 类型(e) 函数类型(f) 切片类型(g) 接口类型(interface)(h) Map 类型

布尔型(boolean type)

布尔型的值只可以是常量 true 或者 false。一个简单的例子:var b bool = true。

数字类型(numeric type)

整型(Integer types)

The predeclared architecture-independent numeric types are:

序号 类型和描述
1 uint8 无符号(unsigned) 8 位整型 (0 到 255)
2 uint16 无符号 16 位整型 (0 到 65535)
3 uint32 无符号 32 位整型 (0 到 4294967295)
4 uint64 无符号 64 位整型 (0 到 18446744073709551615)
5 int8 有符号(signed) 8 位整型 (-128 到 127)
6 int16 有符号 16 位整型 (-32768 到 32767)
7 int32 有符号 32 位整型 (-2147483648 到 2147483647)
8 int64 有符号 64 位整型 (-9223372036854775808 到 9223372036854775807)

There is also a set of predeclared numeric types with implementation-specific sizes:

序号 类型和描述
3 uint 32 或 64 位,由 architecture 决定。如果是32位CPU就是4个字节,如果是64位就是8个字节
4 int 与 uint 一样大小
5 uintptr 无符号整型,用于存放一个指针
uint either 32 or 64 bits
int  same size as uint

Demo

package main

import (
	"fmt"
	"runtime"
	"unsafe"
)

func main() {
	fmt.Println("arch:", runtime.GOARCH)

	var i1 int = 1
	var i2 int8 = 2
	var i3 int16 = 3
	var i4 int32 = 4
	var i5 int64 = 5
	fmt.Println("int:", unsafe.Sizeof(i1))
	fmt.Println("int8:", unsafe.Sizeof(i2))
	fmt.Println("int16:", unsafe.Sizeof(i3))
	fmt.Println("int32:", unsafe.Sizeof(i4))
	fmt.Println("int64:", unsafe.Sizeof(i5))
}

// output:
// arch: amd64
// int: 8
// int8: 1
// int16: 2
// int32: 4
// int64: 8

浮点型(Floating-point Numbers)

序号 类型 描述
1 float32 IEEE-754 32位浮点型数
2 float64 IEEE-754 64位浮点型数
3 complex64 32 位实数(real part)和虚数(imaginary part)
4 complex128 64 位实数和虚数

其他数字类型

以下列出了其他更多的数字类型:

序号 类型和描述
1 byte 类似 uint8
2 rune 类似 int32

字符串类型(String Type)

字符串就是一串固定长度的字符连接起来的字符序列。Go 的字符串是由单个字节连接起来的。Go 语言的字符串的字节使用 UTF-8 编码标识 Unicode 文本。

Strings are immutable: once created, it is impossible to change the contents of a string.

The length of a string s can be discovered using the built-in function len. The length is a compile-time constant if the string is a constant. A string’s bytes can be accessed by integer indices 0 through len(s)-1. It is illegal to take the address of such an element; if s[i] is the i‘th byte of a string, &s[i] is invalid.

派生类型(Derived Types)

包括:

  • 切片类型(slice types)
  • 数组类型(array types)
  • 结构类型(struct types)
  • 指针类型(pointer types)
  • 函数类型(method types)
  • 接口类型(interface types)
  • map 类型 (map types)
  • Channel 类型(channel types)

切片类型(Slice Types)

A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The number of elements is called the length of the slice and is never negative. The value of an uninitialized slice is nil.

数组类型(Array Types)

An array is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length of the array and is never negative.

结构类型(Struct Types)

A struct is a sequence of named elements, called fields, each of which has a name and a type. Field names may be specified explicitly (IdentifierList) or implicitly (EmbeddedField). Within a struct, non-blank field names must be unique.

指针类型(Pointer Types)

A pointer type denotes the set of all pointers to variables of a given type, called the base type of the pointer. The value of an uninitialized pointer is nil.

函数类型(Method Types)

A function type denotes the set of all functions with the same parameter and result types. The value of an uninitialized variable of function type is nil.

接口类型(Interface Types)

An interface type specifies a method set called its interface. A variable of interface type can store a value of any type with a method set that is any superset of the interface. Such a type is said to implement the interface. The value of an uninitialized variable of interface type is nil.

map 类型 (Map Types)

A map is an unordered group of elements of one type, called the element type, indexed by a set of unique keys of another type, called the key type. The value of an uninitialized map is nil.

Channel 类型(Channel Types)

A channel provides a mechanism for concurrently executing functions to communicate by sending and receiving values of a specified element type. The value of an uninitialized channel is nil.

Reference