当前位置: 首页 > news >正文

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

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 团队开发的&#xff0c;并且是一个自由软件&#xff0c;广泛应用于世界各地的产品和项目中【9†…...

Centos如何配置阿里云的yum仓库作为yum源?

背景 Centos在国内访问官方yum源慢&#xff0c;可以用国内的yum源&#xff0c;本文以阿里云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详细题解)

题目链接&#xff1a;139. 单词拆分 - 力扣&#xff08;LeetCode&#xff09; 前情提要&#xff1a; 因为本人最近都来刷dp类的题目所以该题就默认用dp方法来做。 最近刚学完背包&#xff0c;所以现在的题解都是以背包问题为基础再来写的。 如果大家不懂背包问题的话&#…...

CSS —— display属性

用于指定一个元素在页面中的显示方式 HTML中标签元素大体被分为三种类型&#xff1a;块元素、行内元素和行内块元素 块元素 &#xff1a;block 1.独占一行 2.水平方向&#xff0c;占满它父元素的可用空间&#xff08;宽度是父级的100%&#xff09; 3.垂直方向&#xff0c;占据的…...

BTC ETF资金流入暴涨400%,市场下一步将如何发展?

近期&#xff0c;BTC现货ETF&#xff08;交易所交易基金&#xff09;市场出现了显著的资金流入&#xff0c;尤其是在9月10日&#xff0c;BTC ETF吸引了近1.17亿美元的资金流入&#xff0c;相较于前一天的3729万美元&#xff0c;暴涨了400%。这种现象引发了市场广泛关注&#xf…...

视频监控管理平台LntonAIServer视频智能分析抖动检测算法应用场景

在视频监控系统中&#xff0c;视频画面的稳定性对于确保监控效果至关重要。抖动现象是指视频画面中存在不稳定或频繁晃动的情况&#xff0c;这可能会影响视频的清晰度和可读性。LntonAIServer通过引入抖动检测功能&#xff0c;帮助用户及时发现并解决视频流中的抖动问题&#x…...

初识php库管理工具composer的体验【爽】使用phpword模板功能替换里面的字符串文本

需求&#xff1a; 做了一个租赁的项目&#xff0c;里面要求签署个人授权协议&#xff0c;里面要填写姓名&#xff0c;手机号&#xff0c;身份证号&#xff0c;签署日期等参数&#xff0c;格式如下图 格式&#xff1a; 如上图&#xff0c;word中的字符串模板变量使用${varname…...

每日一问:C++ 如何实现继承、封装和多态

每日一问&#xff1a;C 如何实现继承、封装和多态 C 是一门面向对象编程语言&#xff0c;通过继承、封装和多态这三个核心特性实现了对复杂系统的高效管理和扩展。继承让代码重用性得以提升&#xff0c;封装保护数据的完整性&#xff0c;而多态通过不同的接口实现了灵活性。本文…...

STM32常用数据采集滤波算法

例如&#xff0c;STM32进行滤波处理时&#xff0c;主要目的是处理数据采集过程中可能产生的噪声和尖刺信号。这些噪声可能来自电源干扰、传感器自身的不稳定性或其他外部因素。 1.一阶互补滤波 方法&#xff1a;取a0~1,本次滤波结果&#xff08;1-a&#xff09;本次采样值a上…...

二分系列(二分查找)9/12

一、分情况讨论 1.左闭右闭:[left,right] 因为是左闭右闭&#xff0c;所以left和right都能直接取到。 #这里将>放到一起&#xff0c;当nums[mid]>target的时候&#xff0c; 要更新右边界&#xff0c;rightmid-1,这样就把一些相同的情况也切出去了 可以理解为找的第一个…...

如何通过可视化大屏,助力智慧城市的“城市微脑”建设?

在智慧城市的宏伟蓝图中&#xff0c;常常面临着一个关键挑战&#xff1a;如何确保这些理念和技术能够真正地惠及城市的每一个角落&#xff0c;每一个产业&#xff0c;以及每一位市民。问题的核心在于城市的具体应用场景&#xff0c;无论是横向的社区、园区、镇街、学校、酒店、…...

何时空仓库

某仓库现存货物 s 箱&#xff0c;每天上午出货 m 箱、下午进货 n 箱&#xff0c;若s≥m>n≥0&#xff0c;则第 k 天将会出现空仓的情况。请你帮仓库管理员编写程序&#xff0c;输入s、m 和 n&#xff0c;计算并输出 k。 输入格式 s,m,n (s≥m>n≥0) 输出格式 k 输入样例…...

美创获评CNVD年度原创漏洞发现贡献单位!

9月10日&#xff0c;第21届中国网络安全年会暨网络安全协同治理分论坛在广州成功举办。会上&#xff0c;美创科技首次获评“CNVD年度原创漏洞发现贡献单位”。 美创科技依托第59号安全实验室&#xff0c;专注数据安全技术和攻防研究。凭借深厚的技术积累与优势&#xff0c;被遴…...

Spring 循环依赖原理及解决方案

一、什么是循环依赖 循环依赖指的是一个实例或多个实例存在相互依赖的关系&#xff08;类之间循环嵌套引用&#xff09;。 举例&#xff1a; Component public class AService {// A中注入了BAutowiredprivate BService bService; }Component public class BService {// B中也…...

【数据结构与算法 | 灵神题单 | 插入链表篇】力扣2807, LCR 029, 147

1. 力扣2807&#xff1a;在链表中插入最大公约数 1.1 题目&#xff1a; 你一个链表的头 head &#xff0c;每个结点包含一个整数值。 在相邻结点之间&#xff0c;请你插入一个新的结点&#xff0c;结点值为这两个相邻结点值的 最大公约数 。 请你返回插入之后的链表。 两个…...

《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析 (一)

CSI-2 协议详细解析 (一&#xff09; 1. CSI-2层定义&#xff08;CSI-2 Layer Definitions&#xff09; 分层结构 &#xff1a;CSI-2协议分为6层&#xff1a; 物理层&#xff08;PHY Layer&#xff09; &#xff1a; 定义电气特性、时钟机制和传输介质&#xff08;导线&#…...

【SpringBoot】100、SpringBoot中使用自定义注解+AOP实现参数自动解密

在实际项目中,用户注册、登录、修改密码等操作,都涉及到参数传输安全问题。所以我们需要在前端对账户、密码等敏感信息加密传输,在后端接收到数据后能自动解密。 1、引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId...

UDP(Echoserver)

网络命令 Ping 命令 检测网络是否连通 使用方法: ping -c 次数 网址ping -c 3 www.baidu.comnetstat 命令 netstat 是一个用来查看网络状态的重要工具. 语法&#xff1a;netstat [选项] 功能&#xff1a;查看网络状态 常用选项&#xff1a; n 拒绝显示别名&#…...

从深圳崛起的“机器之眼”:赴港乐动机器人的万亿赛道赶考路

进入2025年以来&#xff0c;尽管围绕人形机器人、具身智能等机器人赛道的质疑声不断&#xff0c;但全球市场热度依然高涨&#xff0c;入局者持续增加。 以国内市场为例&#xff0c;天眼查专业版数据显示&#xff0c;截至5月底&#xff0c;我国现存在业、存续状态的机器人相关企…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

相机从app启动流程

一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...

css的定位(position)详解:相对定位 绝对定位 固定定位

在 CSS 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…...

Unit 1 深度强化学习简介

Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库&#xff0c;例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体&#xff0c;比如 SnowballFight、Huggy the Do…...

Element Plus 表单(el-form)中关于正整数输入的校验规则

目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入&#xff08;联动&#xff09;2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...

AI书签管理工具开发全记录(十九):嵌入资源处理

1.前言 &#x1f4dd; 在上一篇文章中&#xff0c;我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源&#xff0c;方便后续将资源打包到一个可执行文件中。 2.embed介绍 &#x1f3af; Go 1.16 引入了革命性的 embed 包&#xff0c;彻底改变了静态资源管理的…...