Go语言现代web开发05 指针和结构体
指针
Pointers are complex data types that store the memory address of value. Simply put, if we have a value stored in the memory address as 100 and a pointer to that value, the pointer value will be 100. The default value for a pointer is nil. Nil pointer does not point to any value.
指针是存储值的内存地址的复杂数据类型。简单地说,如果在内存地址中存储了一个值100和一个指向该值的指针,那么指针的值就是100。指针的默认值是nil。Nil指针不指向任何值。
To declare a pointer, we must add *(asterisk) before type. This will declare an integer pointer:
要声明指针,必须在type前面加上“*(星号)”。声明一个整型指针:
var pi *int
Go has two pointer operators:
- Operator
&(ampersand)will get the address of the variable. We use this operator to initialize or assign a value for a pointer (i is an integer variable):pi := &i. - Operator
*(asterisk)will get us access to the pointed value. We can use it for the following operators:- Read value hrough pointer:
i := *pi - Set value through pointer:
*pi = 33
- Read value hrough pointer:
Go有两个指针操作符:
- 操作符“&(&)”将获得变量的地址。使用该操作符为指针(i为整型变量)初始化或赋值:
pi:= &i。 - 操作符“*(星号)”将使我们访问指向的值。可以将其用于以下操作符:
- 通过指针读取值:
i:= *pi - 通过指针设置值:
*pi = 33
- 通过指针读取值:
In the following example, we will use a pointer to change the value of the integer variable (value 27 will be displayed on the standard output at the end):
在下面的例子中,我们将使用一个指针来改变整型变量的值(值27将在最后的标准输出中显示):
func main(){var i int = 18var pi *intpi = &i*pi = 27fmt.Println(i)
}
Some programming languages support pointer arithmetic. Pointer arithmetic allows us to perform simple arithmetic operations such as increment, decrement, addition, and subtraction. These operations will not change pointed values, but they will change the pointer value. Let’s assume that the pointer points to memory address 100. If we perform an increment arithmetic operation on a pointer, it will now point to address 101. Designers of the Go programming language wanted to keep things as simple as possible. So, they decided through unsafe package. As the name suggests, usage of this package is not recommended and should be avoided(if possible).
一些编程语言支持指针运算。指针算术允许我们执行简单的算术操作,如自增、自减、加法和减法。这些操作不会改变指针指向的值,但会改变指针地址的值。让我们假设指针指向内存地址100。如果对指针执行自增算术运算,它现在将指向地址101。Go编程语言的设计者希望尽可能地保持简单。所以,他们决定通过不安全的包装。顾名思义,不建议使用这个包,应该避免使用(如果可能的话)。
结构体
Struct (shortenedof structure) is a complex data type that can be defined as a collection of fields. Fields can be of different types. We use type and struct keywords to declare struct, with the name in between them. Fields are declared of betweencurly brackets in classical name-type declaration style. Here is an example of struct person which has two fields, name and age.
结构是一种复杂的数据类型,可以定义为字段的集合。字段可以是不同的类型。我们使用type和struct关键字来声明struct,在它们之间加上名称。以经典的名称类型声明风格在括号之间声明字段。下面是一个结构体person的例子,它有两个字段:name和age。
type person struct {name stringage int
}
We can access struct fields with the .(dot) operator. We can access the field in order to set, update, or read its value. This example will update the age of the created person.
我们可以使用.(点)操作符访问结构体字段。我们可以访问字段来设置、更新或读取它的值。这个示例将更新所创建人员的年龄。
p := person{name: "Mara",age: 27,
}
p.age = 33
We can declare a pointer to a struct and use the pointer to access fields. If we follow all pointer rules described in the previous section, the statement that will update the value of the field age should be (pp is pointer to person):
可以声明指向结构体的指针,并使用该指针访问字段。如果我们遵循上一节中描述的所有指针规则,将更新字段age值的语句应该是(pp是指向person的指针):
(*pp).age = 35
The previous statement is a little bit complex and unreadable. So, in Go, the design statement is simplified.
前面的语句有点复杂,难以读懂。因此,在Go中,设计语句被简化了。
pp.age = 33
In the example where we created person, we provided values for all fields. In such situations, we can omit the field name from the definition. Also, we can provide values only for certain fields(in this case, we cannot omit field names) or omit values for all fields. Default values will be assigned to the omitted fields.
在我们创建person的示例中,我们为所有字段提供了值。在这种情况下,我们可以从定义中省略字段名。此外,我们可以仅为某些字段提供值(在这种情况下,我们不能省略字段名)或省略所有字段的值。默认值将分配给省略的字段。
Here are a couple of examples of how we can define struct variable:
下面是几个如何定义struct变量的例子:
p1 := person{"Mara", 33}
p2 := person{name:"Mara"}
p3 := person{}
One important thing, export rules are also applied to struct fields. Even if struct itself is exported, if the field is not exported, other packages will not be able to access it.
重要的一点是,导出规则也适用于结构字段。即使struct本身被导出,如果字段没有被导出,其他包也不能访问它。
相关文章:
Go语言现代web开发05 指针和结构体
指针 Pointers are complex data types that store the memory address of value. Simply put, if we have a value stored in the memory address as 100 and a pointer to that value, the pointer value will be 100. The default value for a pointer is nil. Nil pointer…...
Postgresql 删除数组中的元素
extra为 {“a”: [null, 3, null],“b”: 111} 使用sql 将extra中a中的null移除 第一步: 首先先把[null, 3, null]移除, select json_agg(elem) filter ( where elem ! null ) from (select jsonb_array_elements([null,3,null]::jsonb) as elem) t;这…...
docker 多服务只暴露一个客户端
业务场景 docker部署多个服务时候,当为了安全考虑 部署了多个服务,数据库,缓存库,文件服务器啥的,如果全都暴露的话可能会增加资源侵入的风险,所以只需要挂载一个客户端端口给外部访问即可,其他服务均在内网,保障资源安全 docker 网络 可以把容器们都放在同一网络下,由于docke…...
DFS算法专题(二)——穷举vs暴搜vs深搜vs回溯vs剪枝【OF决策树】
目录 1、决策树 2、算法实战应用【leetcode】 2.1 题一:全排列 2.2.1 算法原理 2.2.2 算法代码 2.2 题二:子集 2.2.1 算法原理【策略一】 2.2.2 算法代码【策略一】 2.2.3 算法原理【策略二,推荐】 2.2.4 算法代码【策略二&#x…...
Spring Security 快速开始
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency> 一、认证 1、从数据中读数据完成认证 Service public class MyUserDetailsService implements UserDeta…...
Lua5.3 参考手册
《Lua 5.3 参考手册》是对 Lua 5.3 版本语言的官方定义。这份手册详细描述了 Lua 语言的语法、语义以及标准库和 C API。它是由巴西里约热内卢 Pontifical Catholic 大学的 PUC-Rio 团队开发的,并且是一个自由软件,广泛应用于世界各地的产品和项目中【9†…...
Centos如何配置阿里云的yum仓库作为yum源?
背景 Centos在国内访问官方yum源慢,可以用国内的yum源,本文以阿里云yum源为例说明。 快速命令 sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.a…...
力扣139-单词拆分(Java详细题解)
题目链接:139. 单词拆分 - 力扣(LeetCode) 前情提要: 因为本人最近都来刷dp类的题目所以该题就默认用dp方法来做。 最近刚学完背包,所以现在的题解都是以背包问题为基础再来写的。 如果大家不懂背包问题的话&#…...
CSS —— display属性
用于指定一个元素在页面中的显示方式 HTML中标签元素大体被分为三种类型:块元素、行内元素和行内块元素 块元素 :block 1.独占一行 2.水平方向,占满它父元素的可用空间(宽度是父级的100%) 3.垂直方向,占据的…...
BTC ETF资金流入暴涨400%,市场下一步将如何发展?
近期,BTC现货ETF(交易所交易基金)市场出现了显著的资金流入,尤其是在9月10日,BTC ETF吸引了近1.17亿美元的资金流入,相较于前一天的3729万美元,暴涨了400%。这种现象引发了市场广泛关注…...
视频监控管理平台LntonAIServer视频智能分析抖动检测算法应用场景
在视频监控系统中,视频画面的稳定性对于确保监控效果至关重要。抖动现象是指视频画面中存在不稳定或频繁晃动的情况,这可能会影响视频的清晰度和可读性。LntonAIServer通过引入抖动检测功能,帮助用户及时发现并解决视频流中的抖动问题&#x…...
初识php库管理工具composer的体验【爽】使用phpword模板功能替换里面的字符串文本
需求: 做了一个租赁的项目,里面要求签署个人授权协议,里面要填写姓名,手机号,身份证号,签署日期等参数,格式如下图 格式: 如上图,word中的字符串模板变量使用${varname…...
每日一问:C++ 如何实现继承、封装和多态
每日一问:C 如何实现继承、封装和多态 C 是一门面向对象编程语言,通过继承、封装和多态这三个核心特性实现了对复杂系统的高效管理和扩展。继承让代码重用性得以提升,封装保护数据的完整性,而多态通过不同的接口实现了灵活性。本文…...
STM32常用数据采集滤波算法
例如,STM32进行滤波处理时,主要目的是处理数据采集过程中可能产生的噪声和尖刺信号。这些噪声可能来自电源干扰、传感器自身的不稳定性或其他外部因素。 1.一阶互补滤波 方法:取a0~1,本次滤波结果(1-a)本次采样值a上…...
二分系列(二分查找)9/12
一、分情况讨论 1.左闭右闭:[left,right] 因为是左闭右闭,所以left和right都能直接取到。 #这里将>放到一起,当nums[mid]>target的时候, 要更新右边界,rightmid-1,这样就把一些相同的情况也切出去了 可以理解为找的第一个…...
如何通过可视化大屏,助力智慧城市的“城市微脑”建设?
在智慧城市的宏伟蓝图中,常常面临着一个关键挑战:如何确保这些理念和技术能够真正地惠及城市的每一个角落,每一个产业,以及每一位市民。问题的核心在于城市的具体应用场景,无论是横向的社区、园区、镇街、学校、酒店、…...
何时空仓库
某仓库现存货物 s 箱,每天上午出货 m 箱、下午进货 n 箱,若s≥m>n≥0,则第 k 天将会出现空仓的情况。请你帮仓库管理员编写程序,输入s、m 和 n,计算并输出 k。 输入格式 s,m,n (s≥m>n≥0) 输出格式 k 输入样例…...
美创获评CNVD年度原创漏洞发现贡献单位!
9月10日,第21届中国网络安全年会暨网络安全协同治理分论坛在广州成功举办。会上,美创科技首次获评“CNVD年度原创漏洞发现贡献单位”。 美创科技依托第59号安全实验室,专注数据安全技术和攻防研究。凭借深厚的技术积累与优势,被遴…...
Spring 循环依赖原理及解决方案
一、什么是循环依赖 循环依赖指的是一个实例或多个实例存在相互依赖的关系(类之间循环嵌套引用)。 举例: Component public class AService {// A中注入了BAutowiredprivate BService bService; }Component public class BService {// B中也…...
【数据结构与算法 | 灵神题单 | 插入链表篇】力扣2807, LCR 029, 147
1. 力扣2807:在链表中插入最大公约数 1.1 题目: 你一个链表的头 head ,每个结点包含一个整数值。 在相邻结点之间,请你插入一个新的结点,结点值为这两个相邻结点值的 最大公约数 。 请你返回插入之后的链表。 两个…...
(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)
题目:3442. 奇偶频次间的最大差值 I 思路 :哈希,时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况,哈希表这里用数组即可实现。 C版本: class Solution { public:int maxDifference(string s) {int a[26]…...
【网络】每天掌握一个Linux命令 - iftop
在Linux系统中,iftop是网络管理的得力助手,能实时监控网络流量、连接情况等,帮助排查网络异常。接下来从多方面详细介绍它。 目录 【网络】每天掌握一个Linux命令 - iftop工具概述安装方式核心功能基础用法进阶操作实战案例面试题场景生产场景…...
日语AI面试高效通关秘籍:专业解读与青柚面试智能助攻
在如今就业市场竞争日益激烈的背景下,越来越多的求职者将目光投向了日本及中日双语岗位。但是,一场日语面试往往让许多人感到步履维艰。你是否也曾因为面试官抛出的“刁钻问题”而心生畏惧?面对生疏的日语交流环境,即便提前恶补了…...
反向工程与模型迁移:打造未来商品详情API的可持续创新体系
在电商行业蓬勃发展的当下,商品详情API作为连接电商平台与开发者、商家及用户的关键纽带,其重要性日益凸显。传统商品详情API主要聚焦于商品基本信息(如名称、价格、库存等)的获取与展示,已难以满足市场对个性化、智能…...
ESP32读取DHT11温湿度数据
芯片:ESP32 环境:Arduino 一、安装DHT11传感器库 红框的库,别安装错了 二、代码 注意,DATA口要连接在D15上 #include "DHT.h" // 包含DHT库#define DHTPIN 15 // 定义DHT11数据引脚连接到ESP32的GPIO15 #define D…...
家政维修平台实战20:权限设计
目录 1 获取工人信息2 搭建工人入口3 权限判断总结 目前我们已经搭建好了基础的用户体系,主要是分成几个表,用户表我们是记录用户的基础信息,包括手机、昵称、头像。而工人和员工各有各的表。那么就有一个问题,不同的角色…...
P3 QT项目----记事本(3.8)
3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...
Ascend NPU上适配Step-Audio模型
1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统,支持多语言对话(如 中文,英文,日语),语音情感(如 开心,悲伤)&#x…...
力扣-35.搜索插入位置
题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...
九天毕昇深度学习平台 | 如何安装库?
pip install 库名 -i https://pypi.tuna.tsinghua.edu.cn/simple --user 举个例子: 报错 ModuleNotFoundError: No module named torch 那么我需要安装 torch pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple --user pip install 库名&#x…...
