当前位置:网站首页>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))
}
边栏推荐
猜你喜欢

Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1

Rich text editor Tinymce

Centos7 install mysql5.7

SQLServer2019安装(Windows)

怎样使用浏览器静默打印网页

Make your own dataset in FCN and train it

redis-企业级使用

半个月时间把MySQL重新巩固了一遍,梳理了一篇几万字 “超硬核” 文章!

SQL——左连接(Left join)、右连接(Right join)、内连接(Inner join)

小程序如何使用订阅消息(PHP代码+小程序js代码)
随机推荐
Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1
cocoaPods管理之后工程结构变化
可以用聚酯树脂将接线板密封接线盒吗?(接线盒灌封胶用哪种树脂)
逆置问题--重点
金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--开放虚拟网络(OVN)简介(课后练习)
【LeetCode】387. 字符串中的第一个唯一字符
Dart Log工具类
单点登录的三种方式
Redis缓冲穿透和缓冲击穿工具类的封装
NowCoderTOP23-27二叉树遍历——持续更新ing
因存在自燃安全隐患,宝马7系和5系紧急召回,合计超过5.7万辆
项目管理工具之燃尽图:动态考核团队工作能力
(C language) program environment and preprocessing
使用turtle画按钮
Creation of doubly linked list
SQLServer2019安装(Windows)
Chapter Six
怎样使用浏览器静默打印网页
【23提前批】北森云计算-测开面经
Redis的简单使用