当前位置: 首页 > 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;结点值为这两个相邻结点值的 最大公约数 。 请你返回插入之后的链表。 两个…...

嵌入式开发中C语言能力层级与核心技术解析

C语言在嵌入式开发中的能力层级解析1. C语言在嵌入式系统中的地位C语言作为嵌入式系统开发的核心语言&#xff0c;其重要性不言而喻。从微控制器编程到操作系统内核开发&#xff0c;C语言凭借其接近硬件的特性、高效的执行效率和丰富的生态系统&#xff0c;成为嵌入式开发领域不…...

硬件突破:用OpenCore Legacy Patcher实现旧Mac的焕新体验

硬件突破&#xff1a;用OpenCore Legacy Patcher实现旧Mac的焕新体验 【免费下载链接】OpenCore-Legacy-Patcher 体验与之前一样的macOS 项目地址: https://gitcode.com/GitHub_Trending/op/OpenCore-Legacy-Patcher OpenCore Legacy Patcher是一款强大的开源工具&#…...

Java 开发 日志技术

1.概述为什么要在程序中记录日志呢&#xff1f;便于追踪应用程序中的数据信息、程序的执行过程。便于对应用程序的性能进行优化。便于应用程序出现问题之后&#xff0c;排查问题&#xff0c;解决问题。便于监控系统的运行状态。2.日志框架JUL&#xff1a;这是JavaSE平台提供的官…...

探索数字微流控:OpenDrop开源平台完全实践

探索数字微流控&#xff1a;OpenDrop开源平台完全实践 【免费下载链接】OpenDrop Open Source Digital Microfluidics Bio Lab 项目地址: https://gitcode.com/gh_mirrors/ope/OpenDrop OpenDrop作为一款基于电润湿技术的开源数字微流控平台&#xff0c;致力于为开源实验…...

通达信缠论画线主图实战:手把手教你5分钟搞定中枢识别与趋势线绘制

通达信缠论画线实战指南&#xff1a;5分钟掌握中枢识别与趋势线绘制技巧 在股票技术分析领域&#xff0c;缠论因其独特的结构思维和实战价值备受投资者青睐。而通达信作为国内主流证券分析软件&#xff0c;其内置的画线工具与缠论理论结合&#xff0c;能够帮助投资者快速识别关…...

如何在Linux内核中实现高性能exFAT文件系统读写支持?

如何在Linux内核中实现高性能exFAT文件系统读写支持&#xff1f; 【免费下载链接】exfat-nofuse Android ARM Linux non-fuse read/write kernel driver for exFat and VFat Android file systems 项目地址: https://gitcode.com/gh_mirrors/ex/exfat-nofuse 你是否曾经…...

图像标注难题如何破解?LabelImg工具全面解析与实战指南

图像标注难题如何破解&#xff1f;LabelImg工具全面解析与实战指南 【免费下载链接】labelImg LabelImg is now part of the Label Studio community. The popular image annotation tool created by Tzutalin is no longer actively being developed, but you can check out L…...

懒人精灵实战:用Lua脚本读写安卓手游内存(以libunity.so为例)

懒人精灵实战&#xff1a;用Lua脚本读写安卓手游内存&#xff08;以libunity.so为例&#xff09; 在移动游戏开发与逆向工程领域&#xff0c;内存读写技术一直是一个既神秘又实用的技能。对于想要深入了解游戏机制或进行自动化测试的开发者来说&#xff0c;掌握这项技术无疑会带…...

终极指南:如何用BongoCat打造你的个性化桌面互动伙伴

终极指南&#xff1a;如何用BongoCat打造你的个性化桌面互动伙伴 【免费下载链接】BongoCat 让呆萌可爱的 Bongo Cat 陪伴你的键盘敲击与鼠标操作&#xff0c;每一次输入都充满趣味与活力&#xff01; 项目地址: https://gitcode.com/gh_mirrors/bong/BongoCat 你是否厌…...

AI人脸隐私卫士效果展示:看它如何精准识别并模糊多人合照

AI人脸隐私卫士效果展示&#xff1a;看它如何精准识别并模糊多人合照 1. 效果展示&#xff1a;从家庭合影到百人合照 1.1 家庭聚会照片处理 想象一下这样的场景&#xff1a;你刚刚参加完一场热闹的家庭聚会&#xff0c;手机里存满了欢乐的合影。这些照片中&#xff0c;有近景…...