Go语言现代web开发05 指针和结构体

news/2024/9/19 0:04:29/ 标签: golang, 前端, 开发语言

指针

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本身被导出,如果字段没有被导出,其他包也不能访问它。


http://www.ppmy.cn/news/1526163.html

相关文章

重修设计模式-创建型-建造者模式

重修设计模式-创建型-建造者模式 允许用户通过链式调用方法来逐步构建复杂对象,让复杂对象的构建与它的表示分离,即对象的表示和对象的构造过程解耦。 建造者模式的原理和实现非常简单,重点在于复杂对象的构建过程和定制化。具体实现中&#…

OkHttp Interceptor日志上报

最近为了做一些网络上的优化,所以就得提前埋点,为后续网络优化提供数据支持。 主要是对发起请求埋点,请求错误埋点,客户端请求耗时埋点。 事件上报到阿里云,接入的是阿里的应用实时监控服务。 网络请求使用的是OhHttp…

【移动端】Flutter与uni-app:全方位对比分析

文章目录 一、含义1. Flutter2. uni-app 二、开发程序步骤1. Flutter2. uni-app 三、基本语言区别四、优缺点1. Flutter2. uni-app优点:缺点: 五、如何选型 一、含义 1. Flutter Flutter是由Google开发的一款跨平台移动应用开发框架,采用Da…

怎么选择适合的服务器

大家都知道,不管是公司还是个人,在数字化浪潮已经席卷全球的环境下,大家对服务器的需求是日渐增长的。很多人在买服务器的时候,多少都有点选择困难,今天我们就来对比下物理服务器和弹性云服务器,看看选哪个…

【leetcode C++】动态规划

16. 123. 买股票的最佳时机3 题目: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 注意:你不能同时参与多笔交易(你必须在再次购买前出售…

Linux下write函数

在 Linux 中,write 函数是操作系统提供的最基础的系统调用之一,用于向文件描述符写入数据。它的使用非常广泛,不仅仅限于普通文件,还包括管道、套接字、字符设备等。 Linux 中的 write 函数详解 一、函数定义与头文件 write 函…

C# winform 字符串模糊查询,也就是查找子串

C# winform 字符串模糊查询,也就是查找子串。 1. String.Contains() Contains() 方法通常使用内部的 IndexOf() 实现,所以它的性能与 IndexOf() 相近。这是一个非常快速的方法,适合于一般的应用场景。 2. String.IndexOf() IndexOf() 方法…

性能测试的复习3-jmeter的断言、参数化、提取器

一、断言、参数化、提取器 需求: 提取查天气获取城市名请求的响应结果:城市对查天气获取城市名的响应结果进行响应断言和json断言对查天气获取城市名添加用户参数 1、步骤 查看天气获取城市名 json提取器(对响应结果提取、另一个接口请求…

Graphviz 安装并使用 (Python)

概述 Graphviz 是一款由 AT&T Research 和 Lucent Bell 实验室开源的可视化图形工具,可以很方便的用来绘制结构化的图形网络,支持多种格式输出。Graphviz 输入是一个用 dot 语言编写的绘图脚本,通过对输入脚本的解析,分析出其…

Android NDK工具

Android NDK工具 Android NDK Crash 日志抓取及定位 NDK-STACK 定位 NDK Crash 位置 只要执行如下代码就行: adb logcat | ndk-stack -sym /yourProjectPath/obj/local/armeabi-v7aPS: 必须是带symbols的so,也就是在’\app\src\main\obj\local\下面的…

Kernel 内核 BUG_ON()和WARN_ON()

WARN_ON() DEBUG_ON() 不是一个标准的 Linux 内核宏,它可能是特定内核版本或者特定内核配置中的一个宏,或者在某些内核代码中自定义的宏。一般来说,如果存在 DEBUG_ON(),它可能被用作一个调试开关,用于在调试版本中启…

判断链表的全部n个字符是否中心对称。

设单链表的表头指针为L,结点结构由data和next两个域构成,其中data域为字符型。试设计算法判断链表的全部n个字符是否中心对称。例如XYX,XYYX是中心对称。 思想:先求链表长度,然后将前n/2个元素入栈,将栈顶…

鸿蒙NEXT生态应用核心技术理念:一次开发,多端部署

在万物互联时代,应用开发者也面临设备底座从手机单设备到全场景多设备的转变,通过全场景多设备作为全新的底座,为消费者带来万物互联时代更为高效、便捷的体验。 在万物智联时代重要机遇期,鸿蒙结合移动生态发展的趋势&#xff0…

数据结构——(java版)Map与Set

文章目录 二叉搜索树(1) 二叉搜索树的概念:(2)二叉搜索树的意义:(3)二叉搜索树的实现:实现的方法与属性实现二叉搜索树的查询:实现二叉搜索树的插入&#xff…

`character_set_server` 和 `collation_server`

目录标题 1. character_set_server 的取值范围和相关性取值范围相关性 2. collation_server 的取值范围和相关性取值范围相关性 3. 默认值4. 配置方法5. 注意事项MySQL中character_set_server和collation_server参数修改对现有数据库和表的具体影响是什么?如何在不中…

ansible企业实战

ansible最佳实践 优化ansible速度 开启SSH长连接 修改 /etc/ansible/ansible.cfg里面的参数 ssh_args -C -o ControlMasterauto -o ControlPersist5d ControlPersist5d这个参数是设置整个长连接保持时间设置为5天,如果开启,通过SSH连接过的设备都会…

Docker基本管理--Dockerfile镜像制作(Docker技术集群与应用)

容器端口映射; 容器间通信; 容器数据卷; DockerFile; 容器端口映射: 实验环境:紧接着之前的快照,将该文件夹拉取进去; 然后执行导入的脚本,会将该目录下所有打包好的镜像文件导入进入。 然后进…

每日学习一个数据结构-倒排表

文章目录 示意图倒排表的基本概念倒排表的数据结构示例 倒排表的优点应用场景 倒排表(Inverted Index),也称为反向索引或倒排文件,在信息检索系统中是一种重要的数据结构。它主要用于快速搜索文档中的关键词,并找到包含…

Spring Cloud 八股文

目录 Nacos(服务注册、配置中心) Spring Cloud Alibaba中Nacos的核心功能是什么? Nacos 是如何进行服务注册和发现的? 讲述 Nacos 配置中心的工作原理 描述如何通过 Nacos 实现动态配置更新 Spring Cloud Alibaba 中的 Nacos…

Linux学习笔记8 理解Ubuntu网络管理,做自己网络的主人

本文讲解了Ubuntu下网络由什么管理,介绍了临时ip和路由的设置方法,介绍了静态持久化网络配置的方法以及各网络管理软件之间的关系。 来看看Ubuntu网络管理。 序言 原本学习ubuntu网络管理就是为了检查nginx安装过程中使用wget获取压缩包为什么解析不出…