当前位置:网站首页>7 days to learn Go, Go structure + Go range to learn
7 days to learn Go, Go structure + Go range to learn
2022-07-31 10:37:00 【dream eraser】
Write before studying
In the previous blog we learned Go 数组,It requires all elements to be of the same data type,If you want to store different types of data,Knowledge of structures is required.
结构体的定义:Store collections of data of the same or different types.
有 C 相关经验,Structures are relatively easy to understand,语法格式如下所示:
type struct_variable_type struct {
member definition
member definition
...
member definition
}
The keywords in the above syntax format are struct
和 type
,struct_variable_type
是结构体名称,例如我们声明一个【人】的结构体,有姓名,有年龄,有性别.
package main
import "fmt"
// 声明结构体
type People struct {
name string
age int
sex int
}
func main() {
//使用结构体
people := People{
"橡皮擦",
18,
0}
fmt.Println(people)
}
Pay attention to the syntax when writing,The code for using the structure is recommended to be placed on one line,Or a closing brace immediately following the last element.
Using structs can also carry element names,That is, the following writing.
//使用结构体
people := People{
name: "橡皮擦", age: 18, sex: 0}
fmt.Println(people)
访问结构体成员
使用 结构体.成员名
即可,Of course, you can also use this method to assign values.
//使用结构体
var people1 People
// var people2 People
people1.name = "橡皮擦"
people1.age = 18
people1.sex = 1
fmt.Println(people1)
Go Range
作为一个 Python 程序员,关键字 range 是非常熟悉的,在 Go 中 range 关键字可以用于 for 循环,For arrays it returns the index and value of the element,Returns key-value pairs in the subsequently learned collection.
range The syntax format for arrays is as follows:
for i,value := range a_array{
// TODO
}
Combining grammatical formats,Write the following code:
package main
import "fmt"
var a_array = []int{
1, 2, 3, 4, 5, 6, 7, 8}
func main() {
for i, value := range a_array {
fmt.Printf("索引:%d,值:%d\n", i, value)
}
}
Running the code outputs the following information:
索引:0,值:1
索引:1,值:2
索引:2,值:3
索引:3,值:4
索引:4,值:5
索引:5,值:6
索引:6,值:7
索引:7,值:8
如果将 range 作用于字符串,Each character can be iteratively output.
package main
import (
"fmt"
)
func main() {
var str string = "xiangpica"
for k, v := range str {
fmt.Println(k, string(v))
}
}
上述 str
The content is in pure English,k
value every time+1.
0 x
1 i
2 a
3 n
4 g
5 p
6 i
7 c
8 a
如果 str
中包含中文,k
value every time +3,代码如下:
func main() {
var str string = "橡皮擦"
for k, v := range str {
fmt.Println(k, string(v))
}
}
输出结果如下:
0 橡
3 皮
6 擦
If it is a mix of Chinese and English,The results that appear will be more interesting.
0 x
1 i
2 a
3 n
4 g
5 橡
8 p
9 i
10 皮
13 c
14 a
15 擦
There is actually a conclusion here,range
Iteration yes Unicode,而不是字节,返回值是 UTF-8 编码第 1 个字节的索引,So the index values may not be consecutive.
在编写代码的时候,如果不需要索引,Only elements are kept,Deprecated placeholders can be used,代码如下:
var str string = "xiang橡pi皮ca擦"
for _, v := range str {
fmt.Println(string(v))
}
边栏推荐
猜你喜欢
“chmod 777-R 文件名”什么意思?
Insertion and deletion of doubly linked list
Mybaits 常用问题详解
出色的移动端用户验证
学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架
Experience innovation and iteration through the development of a lucky draw applet
GZIPInputStream 类源码分析
《JUC并发编程 - 高级篇》06 - 共享模型之不可变(不可变类的设计 | 不可变类的使用 | 享元模式)
NowCoderTOP17-22 Binary search/sort - continuous update ing
Come n times - 07. Rebuild the binary tree
随机推荐
IBM SPSS Statistics 28软件安装包下载及安装教程
Deletion of the sequence table
Detailed explanation of SQL stored procedures
Burndown chart of project management tools: Dynamic assessment of team work ability
一种用于保证多方子系统数据一致性的方法
解决rpc error: code = Unimplemented desc = method CheckLicense not implemented
KVM virtualization job
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--开放虚拟网络(OVN)简介(课后练习)
Three ways of single sign-on
【LeetCode】118.杨辉三角
WEB核心【记录网站登录人数,记录用户名案例】Cookie技术实现
Web系统常见安全漏洞介绍及解决方案-sql注入
NowCoderTOP17-22 二分查找/排序——持续更新ing
Single sign-on principle and implementation
[ 动词词组 ] 合集
突破传统可靠性测试:混沌工程优秀实践
Implement a thread pool
Android安全专题(三)JNI混淆
SQL存储过程详解
“chmod 777-R 文件名”什么意思?