当前位置:网站首页>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))
}
边栏推荐
- NowCoderTOP28-34二叉树——持续更新ing
- The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
- Burndown chart of project management tools: Dynamic assessment of team work ability
- SQLSERVER merges subquery data into one field
- Redis的简单使用
- Redis缓冲穿透和缓冲击穿工具类的封装
- 项目管理工具之燃尽图:动态考核团队工作能力
- 逆置问题--重点
- 力扣shell刷题
- What is the encoding that starts with ?
猜你喜欢
随机推荐
What does "chmod 777-R filename" mean?
Dart Log tool class
迪拜的超市---线段树双重懒标记+二分
浅谈Attention与Self-Attention,一起感受注意力之美
模块八
使用turtle画按钮
Three ways of single sign-on
Hospital management system database, course design, SQLserver, pure code design
前序、后序及层次遍历实现二叉树的序列化与反序列化
实现弹框组件
Windows安装mysql详细步骤(通俗易懂,简单上手)
[ verb phrase ] collection
如何判断自己是否适合IT行业?方法很简单
nodeJs--url模块
Come n times - 07. Rebuild the binary tree
Day113.尚医通:用户认证、阿里云OSS、就诊人管理
GZIPInputStream 类源码分析
Source code analysis of GZIPInputStream class
[Part 1 of Cloud Native Monitoring Series] A detailed explanation of Prometheus monitoring system
恋爱期间的赠与能否撤销









