当前位置:网站首页>Go语言自学系列 | go语言数据类型
Go语言自学系列 | go语言数据类型
2022-07-01 18:36:00 【51CTO】
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
在 Go 编程语言中,数据类型用于声明函数和变量。
数据类型的出现是为了把数据分成所需内存大小不同的数据,编程的时候需要用大数据的时候才需要申请大内存,就可以充分利用内存。
Go 语言按类别有以下几种数据类型:
序号 | 类型和描述 |
1 | 布尔型 布尔型的值只可以是常量 true 或者 false。一个简单的例子:var b bool = true。 |
2 | 数字类型 整型 int 和浮点型 float32、float64,Go 语言支持整型和浮点型数字,并且支持复数,其中位的运算采用补码。 |
3 | 字符串类型: 字符串就是一串固定长度的字符连接起来的字符序列。Go 的字符串是由单个字节连接起来的。Go 语言的字符串的字节使用 UTF-8 编码标识 Unicode 文本。 |
4 | 派生类型: 包括:(a) 指针类型(Pointer)(b) 数组类型 (c) 结构化类型(struct)(d) Channel 类型 (e) 函数类型 (f) 切片类型 (g) 接口类型(interface)(h) Map 类型 |
数字类型
Go 也有基于架构的类型,例如:int、uint 和 uintptr。
序号 | 类型和描述 |
1 | uint8 无符号 8 位整型 (0 到 255) |
2 | uint16 无符号 16 位整型 (0 到 65535) |
3 | uint32 无符号 32 位整型 (0 到 4294967295) |
4 | uint64 无符号 64 位整型 (0 到 18446744073709551615) |
5 | int8 有符号 8 位整型 (-128 到 127) |
6 | int16 有符号 16 位整型 (-32768 到 32767) |
7 | int32 有符号 32 位整型 (-2147483648 到 2147483647) |
8 | int64 有符号 64 位整型 (-9223372036854775808 到 9223372036854775807) |
浮点型
序号 | 类型和描述 |
1 | float32 IEEE-754 32位浮点型数 |
2 | float64 IEEE-754 64位浮点型数 |
3 | complex64 32 位实数和虚数 |
4 | complex128 64 位实数和虚数 |
其他数字类型
以下列出了其他更多的数字类型:
序号 | 类型和描述 |
1 | byte 类似 uint8 |
2 | rune 类似 int32 |
3 | uint 32 或 64 位 |
4 | int 与 uint 一样大小 |
5 | uintptr 无符号整型,用于存放一个指针 |
举例
package main
import "fmt"
func a3() {
}
func main() {
var name string = "tom"
age := 20
b := true
fmt.Printf("name: %T\n", name)
fmt.Printf("age: %T\n", age)
fmt.Printf("b: %T\n", b)
a := 100
p := &a
fmt.Printf("p: %T\n", p)
a1 := [2]int{1, 2}
fmt.Printf("a1: %T\n", a1)
a2 := []int{1, 2, 3}
fmt.Printf("a2: %T\n", a2)
fmt.Printf("a3: %T\n", a3)
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
运行结果
边栏推荐
- LiveData postValue会“丢”数据
- Lumiprobe Lumizol RNA 提取试剂解决方案
- Lumiprobe lumizol RNA extraction reagent solution
- Force buckle day33
- 如何运营好技术相关的自媒体?
- Leetcode-128 最长连续序列
- C# SelfHost WebAPI (2)
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- Opencv map reading test -- error resolution
- About enterprise middle office planning and it architecture microservice transformation
猜你喜欢
用GSConv+Slim Neck改进Yolov5,将性能提升到极致!
创建您自己的NFT集合并发布一个Web3应用程序来展示它们(介绍)
AI 训练速度突破摩尔定律;宋舒然团队获得RSS 2022最佳论文奖
LeetCode-21合并两个有序链表
Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯
Privacy sandbox is finally coming
Force buckle day33
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
斯坦福、Salesforce|MaskViT:蒙面视觉预训练用于视频预测
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
随机推荐
C# SelfHost WebAPI (2)
Classpath classpath
Roll out! Enlightenment!
Calculation of intersection of two line segments
用GSConv+Slim Neck改进Yolov5,将性能提升到极致!
PMP daily three questions (February 15, 2022)
Reading notes series "modern methods of C language programming" -- Chapter 2
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
R语言ggplot2可视化:gganimate包transition_time函数创建动态散点图动画(gif)、shadow_wake函数配置动画的渐变效果(gradual falloff)拖尾效应
[CF559E]Gerald and Path
[CF1476F]Lanterns
Localization through custom services in the shuttle application
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用n.of.lines参数指定显示的病例数
t10_ Adapting to Market Participantsand Conditions
LeetCode 148. Sort linked list
What if the reliability coefficient is low? How to calculate the reliability coefficient?
Search 2D matrix 2
[acnoi2022] color ball
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
1、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》什么是NFT