当前位置:网站首页>Golang中结构体Struct
Golang中结构体Struct
2022-06-10 05:37:00 【星际迷航‖】
前言
这里总结的Golang 笔记只适合有点编程基础的人看,比如Java
往期内容:
结构体Struct
面向对象
面向对象程序设计(OOP)就是将现实世界中的实物抽象为程序之中的对象。
Golang与Java面向对象区别
- 面向对象编程 OOP,与传统的面向编程有区别,并不是纯粹的面向编程对象语言,golang支持面向对象编程特性。
- Golang之中没有Class,只有struct与class功能一样。
- golang去掉传统面向对象编程语言继承、方法重载、构造函数和析构函数、隐藏的this指针。
- Golang之中有继承、封装、多态的特性,知识实现方式不同,通过匿名字段来实现。
Struct使用
Struct介绍
结构体成员:
- 字段拥有自己的类型和值;
- 字段名必须唯一;
- 字段的类型也可以是结构体,甚至是字段所在结构体的类型。
Struct结构
type typeName struct{
fieldName1 type
fieldName2 type
}
通过如上方式声明一个结构体Struct
type就是类似int、float等类型,在没有初始化的时候,引用类型默认值是nil,值类型就是变量文章里讲的默认值。
Struct四种声明类型
package main
type Student struct {
id int
name string
age int
}
func main() {
// 方式1
stu1 := Student{
1,"James",22}
// 方式2
var stu2 Student
stu2.name = "Kobe"
// 方式3
var stu3 *Student = new(Student)
(*stu3).name = "Curry"
stu3.name = "Curry" // 这种方式底层会处理为上面这种
// 方式4
var stu4 *Student = &Student{
} // {}这个里面可以给字段赋值
stu4.name = "Scott"
}
后面两种方式,Golang在编译的时候会将stu转换为*stu形式。
Struct使用细节
Struct是一个值类型,将一个Struct1赋值给一个新的Struct2,然后新的Struct2中的i值改变为2,实际就是在内存中重新开辟一个空间,把Struct1中信息复制过来,然后将i变更。
以下为内存图
就是结构体之内的空间时连续的,但是结构体指针指向的空间不是连续的。
结构体类型
结构体进行type重新定义(相当于取别名),Golang认为新的数据类型,但是相互之间可以转换
type Student struct {
name string
id int
}
type stu Student
type integer int
func main() {
var stu1 Student
var stu2 stu
stu2 = stu(stu1)
fmt.Println(stu2)
fmt.Printf("%T %T", stu1,stu2)
fmt.Println()
var i integer = 10
var j int = 20
fmt.Printf("%T %T", i,j)
}

从上述可以看出重定义之后,结构体的类型就会发生变化,如果要赋值的话必须强转,如果两个结构体内字段属性一致,那么可以直接将结构体变量进行赋值不需要进行强制转换。
Struct序列化
struct 的每个字段,可以写一个tag 该tag可以通过反射机制获取,常见使用场景就是序列化和反序列化
// 改成小写下面返回空串,json包里访问不了其他包中的字段
type Monster struct {
Name string `json:"name"`
Age int `json:"age"`
Skill string `json:"skill"`
}
func main() {
monster := Monster{
"牛魔王", 20,"芭蕉扇"}
jsonStr, _ := json.Marshal(monster)
fmt.Println(string(jsonStr))
}
如上如果不加tag的话,返回的字段名就是大写。如果不加tag字段名小写返回空字符串,原因是json包访问不到当前包Struct中属性。
如果文章内容有错误,还请大佬们指导,防止误人子弟。继续冲冲冲!
边栏推荐
- Encrypt / decrypt public / private key
- MKS h3615ns DC motor drive instruction manual
- With 80% working hours and 100% salary, Britain officially launched the "four-day working system" experiment!
- IDC released China Cloud native market analysis. Ant group has become one of the most comprehensive manufacturers
- Three principles of layout design
- matlab中不同随机数的生成
- Model lightweight pruning distillation quantification series yolov5 lossless pruning (with source code)
- Several skills of API interface optimization
- 将对象数组转换成二维数组
- Crash使用
猜你喜欢

With 80% working hours and 100% salary, Britain officially launched the "four-day working system" experiment!

Five best! Ant group passed the highest level evaluation of the "stability Assurance Plan" of the ICT Institute

Some operations on mat class in QT

Focus on web development, simple web development

Several skills of API interface optimization

MKS h3615ns DC motor drive instruction manual

Model lightweight pruning distillation quantification series yolov5 lossless pruning (with source code)

蚂蚁集团三项技术方案入选“2021年信息技术应用创新典型解决方案”

Simple and interesting Snake growth game -- greedy snake
![[live dialogue] graph computing is the next frontier of science and technology](/img/23/7477ec2c81866ece291b01db9b7e4b.png)
[live dialogue] graph computing is the next frontier of science and technology
随机推荐
[STM32] Hal library -spi
Ant group's all-in-one privacy computing machine has obtained double certification, and 83 indicators meet the requirements
Builder pattern
leetcode 1332. Remove palindromic subsequences
[nick] intensive reading
matlab中不同随机数的生成
如何保障系统稳定性并实现减排?蚂蚁集团有这些关键技术
最高奖项!2022数博会领先科技成果“新技术”授予OceanBase数据库
In terms of emotional perception, the financial assistant robot "zhixiaobao" goes further
英语太差怎么办,快来写一个“百词斩”软件给自己背单词
《模型轻量化-剪枝蒸馏量化系列》YOLOv5无损剪枝(附源码)
Practice of Flink CDC + Hudi massive data entering the lake in SF
General template for one page test plan of Agile Project
New words new words new words new words new words new words
Api 接口优化的几个技巧
NPM command Encyclopedia
Talk about the importance of technology and tasks
Buuctf hardsql[geek challenge 2019]
树莓派4B编译内核模块
Import Base64 collection and export large pictures