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

透过开发抽奖小程序,体会创新与迭代

Burndown chart of project management tools: Dynamic assessment of team work ability

NowCoderTOP28-34 binary tree - continuous update ing

LeetCode二叉树系列——101.对称二叉树

centos7安装mysql5.7

众多mock工具,这一次我选对了

nodeJs--url模块

Mybaits Frequently Asked Questions Explained

新人学习小熊派华为iot介绍

nodeJs--querystring模块
随机推荐
nodeJs--querystring模块
Sql优化总结!详细!(2021最新面试必问)
[ 动词词组 ] 合集
Redis-基础
实现弹框组件
SQL力扣刷题五
Windows系统Mysql8版本的安装教程
《JUC并发编程 - 高级篇》06 - 共享模型之不可变(不可变类的设计 | 不可变类的使用 | 享元模式)
强大的SQL计算利器-SPL
新人学习小熊派华为iot介绍
【JWT】JWT 整合
C#之泛型、委托、事件及其使用
ASP.NET 身份认证框架 Identity(一)
众多mock工具,这一次我选对了
windows平台下的mysql启动等基本操作
怎样使用浏览器静默打印网页
Redis缓存面临的缓存穿透问题
内网渗透学习(四)域横向移动——SMB和WMI服务利用
Day113.尚医通:用户认证、阿里云OSS、就诊人管理
半个月时间把MySQL重新巩固了一遍,梳理了一篇几万字 “超硬核” 文章!