Go 数据类型
Go - 数据类型
Section titled “Go - 数据类型”Go 中的 data type(数据类型)指定 variable(变量)可以持有的值的种类以及可以对这些值执行的操作。Go 是一种 statically-typed language(静态类型语言),这意味着 variable(变量)的 types(类型)在 compile time(编译时)已知。
Go 的 data types(数据类型)可以大致分类如下:
| 类别 | 描述和示例 |
|---|---|
| Boolean Types(布尔类型) | bool:表示一个 boolean value(布尔值),可以是 true 或 false。Example(示例): var isActive bool = true |
| Numeric Types(数值类型) | 表示 numbers(数字)。细分为: - Integer Types(整数类型): int8、int16、int32、int64、int(有符号);uint8、uint16、uint32、uint64、uint(无符号)。byte 是 uint8 的 alias(别名)。rune 是 int32 的 alias(别名)(表示一个 Unicode code point - 码点)。Example(示例): var age int = 30,var temperature int8 = -5,var unicodeChar rune = 'G'- Floating-Point Types(浮点类型): float32、float64(IEEE-754 标准)。Example(示例): var price float64 = 19.99- Complex Types(复数类型): complex64(float32 实部和虚部),complex128(float64 实部和虚部)。Example(示例): var c complex128 = 3 + 4i |
| String Types(字符串类型) | string:表示一个 byte sequence(字节序列),通常是 UTF-8 编码的文本。Strings 是 immutable(不可变)的。Example(示例): var name string = "Golang" |
| Derived/Composite Types(派生/复合类型) | 由 basic types(基本类型)或其他 composite types(复合类型)构建而成: - Pointer Types(指针类型):例如, *int(一个指向整数的指针)。持有 memory address(内存地址)。- Array Types(数组类型):例如, [5]int(一个包含 5 个整数的数组)。Fixed-size(固定大小)。- Slice Types(切片类型):例如, []int(一个整数切片)。Dynamically-sized view of an array(数组的动态大小视图)。- Structure Types ( struct)(结构体类型):例如,type Point struct { X, Y int }。将 fields(字段)分组。- Function Types(函数类型):表示 function(函数)的 signature(签名)。 - Interface Types(接口类型):定义一组 method signature(方法签名)。 - Map Types(映射类型):例如, map[string]int。无序的 key-value pair(键值对)。- Channel Types(通道类型):例如, chan int。用于 goroutine communication(协程通信)。 |
Integer Types Detailed(整数类型详情)
Section titled “Integer Types Detailed(整数类型详情)”Go 提供了 architecture-independent integer types(与架构无关的整数类型),它们具有 fixed sizes(固定大小),以及 implementation-dependent int and uint types(与实现相关的 int 和 uint 类型)。
| 类型 | 大小 | 范围(近似) |
|---|---|---|
int8 | 8-bit(8 位) | -128 到 127 |
uint8 (byte) | 8-bit(8 位) | 0 到 255 |
int16 | 16-bit(16 位) | -32,768 到 32,767 |
uint16 | 16-bit(16 位) | 0 到 65,535 |
int32 (rune) | 32-bit(32 位) | -21 亿 到 21 亿 |
uint32 | 32-bit(32 位) | 0 到 42 亿 |
int64 | 64-bit(64 位) | -9 百亿亿 到 9 百亿亿 |
uint64 | 64-bit(64 位) | 0 到 18 百亿亿 |
int | 32 or 64 bits(32 或 64 位) | Depends on system architecture(取决于系统架构) |
uint | 32 or 64 bits(32 或 64 位) | Depends on system architecture(取决于系统架构) |
Floating-Point Types Detailed(浮点类型详情)
Section titled “Floating-Point Types Detailed(浮点类型详情)”Go supports standard IEEE-754 floating-point numbers(支持标准的 IEEE-754 浮点数)。
| 类型 | 精度 | 描述 |
|---|---|---|
float32 | Approx. 6-9 decimal digits(约 6-9 位小数) | 32-bit floating-point number(32 位浮点数) |
float64 | Approx. 15-17 decimal digits(约 15-17 位小数) | 64-bit floating-point number (default for literals like 3.14)(64 位浮点数(默认用于字面量如 3.14)) |
Complex Types Detailed(复数类型详情)
Section titled “Complex Types Detailed(复数类型详情)”For complex number arithmetic(用于复数运算)。
| 类型 | Component Type(组件类型) | 描述 |
|---|---|---|
complex64 | float32 | Real and imaginary parts are float32(实部和虚部都是 float32) |
complex128 | float64 | Real and imaginary parts are float64(实部和虚部都是 float64) |
Other Special Numeric Aliases(其他特殊数值别名):
Section titled “Other Special Numeric Aliases(其他特殊数值别名):”byte:Alias foruint8(uint8 的别名)。Commonly used to represent byte data(常用于表示字节数据)。rune:Alias forint32(int32 的别名)。Represents a Unicode code point(表示一个 Unicode 码点),often used when working with characters and strings(常用于处理字符和字符串)。
Additionally(此外),uintptr is an unsigned integer type large enough to store the uninterpreted bits of a pointer value(是一个无符号整数类型,大小足以存储指针值的未解释位)。它的 use is typically confined to low-level programming with the unsafe package(其使用通常仅限于使用 unsafe 包进行低级编程)。
Choosing the right data type is important for memory efficiency and program correctness. For example, using int8 for a value that will never exceed 100 saves memory compared to int64.