当前位置:网站首页>Go语言自学系列 | golang结构体指针
Go语言自学系列 | golang结构体指针
2022-04-23 08:22:00 【COCOgsta】
视频来源:B站《golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]》
一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:Go语言自学系列 | 汇总_COCOgsta的博客-CSDN博客
结构体指针和普通的变量指针相同,我先来回顾一下普通变量的指针,例如:
package main
import "fmt"
func main() {
var name string
name = "tom"
// p_name 指针类型
var p_name *string
// &name 取name地址
p_name = &name
fmt.Printf("name: %v\n", name)
// 输出指针地址
fmt.Printf("p_name: %v\n", p_name)
// 输出指针指向的内容值
fmt.Printf("*p_name: %v\n", *p_name)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
name: tom
p_name: 0xc000050230
*p_name: tom
go结构体指针
实例
package main
import "fmt"
func main() {
type Person struct {
id int
name string
}
var tom = Person{1, "tom"}
var p_person *Person
p_person = &tom
fmt.Printf("tom: %v\n", tom)
fmt.Printf("p_person: %p\n", p_person)
fmt.Printf("*p_person: %v\n", *p_person)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
tom: {1 tom}
p_person: 0xc000004078
*p_person: {1 tom}
使用new关键字创建结构体指针
我们还可以通过使用new关键字对结构体进行实例化,得到的是结构体的地址,例如:
package main
import "fmt"
func main() {
type Person struct {
id int
name string
}
var p_person = new(Person)
fmt.Printf("p_person: %T\n", p_person)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
p_person: *main.Person
从运行结果,我们发现p_person为指针类型
访问结构体指针成员
访问结构体指针成员,也使用点运算符(.),例如:
package main
import "fmt"
func main() {
type Person struct {
id int
name string
}
var p_person = new(Person)
fmt.Printf("p_person: %T\n", p_person)
p_person.id = 1
p_person.name = "tom"
fmt.Printf("*p_person: %v\n", *p_person)
}
运行结果
[Running] go run "d:\SynologyDrive\软件开发\go\golang入门到项目实战\goproject\360duote.com\pro01\test.go"
p_person: *main.Person
*p_person: {1 tom}
版权声明
本文为[COCOgsta]所创,转载请带上原文链接,感谢
https://blog.csdn.net/guolianggsta/article/details/124355572
边栏推荐
- Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
- 洋桃电子STM32物联网入门30步笔记四、工程编译和下载
- Type anonyme (Principes fondamentaux du Guide c)
- 00后最关注的职业:公务员排第二,第一是?
- Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
- 一个必看的微信小程序开发指南1-基础知识了解
- form中enctype属性
- 队列(c语言/链表)
- 微信小程序 catchtap=“toDetail“ 事件问题
- 洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
猜你喜欢

洋桃电子STM32物联网入门30步笔记四、工程编译和下载

Search the complete navigation program source code

作文以记之 ~ 二叉树的后序遍历

Talk about the basic but not simple stock data

Shell script advanced

LeetCode-199-二叉树的右视图

WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code

Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam

ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建

Asan minimalism
随机推荐
Detailed description of self feeling of auricular point weight loss 0422
Asan minimalism
分组背包呀
input元素添加监听事件
Anonymous type (c Guide Basics)
PDF with watermark
四张图弄懂matplotlib的一些基本用法
rembg 分割mask
关于数组复制问题
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
DOM 学习之—添加+-按钮
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
How browser works
一个必看的微信小程序开发指南1-基础知识了解
引用传递1
Enctype attribute in form
JVM工具之Arthas使用
对li类数组对象随机添加特性,并进行排序
Queue (C language / linked list)