当前位置:网站首页>How does golang assign values to empty structures
How does golang assign values to empty structures
2022-07-27 07:10:00 【Tie Zhu】
One 、 Preface
I haven't used less recently golang, I haven't learned systematically , Directly go up and develop , Encounter many pits and learn a lot , This time, the problem of assigning values to empty structures is recorded .
Two 、 Case study
1. Structure structure and error
type Test struct {
Test1 int64 `gorm:"column:test1" json:"test1"`
Test2 string `gorm:"column:test2" json:"test2"
}
var test []*Test
// This way is a failure ? Because it is empty at present , Can't use something like php The way to write dead
test[0].Test1 = xxx
test[0].Test2 = xxxx
Here you can see , I wanted to fill it directly , But life or death cannot be written , Habitual thinking kills people , Online Baidu reason It's an empty structure , Pointer for nil, It doesn't take up memory space , Cannot assign
2. How to assign values to empty structures
(1) The first method
Direct assignment ( It means that this is a testStruct{} type )
test1 :=testStruct{
Test1: xx,
Test2: xx,
}
(2) The second method
test1 := &testStruct{}
test1.Test1 = int64(0)
(3) The third method
test1 :=new(testStruct)
test1.Test1 = int64(0)
(4) About new and make The difference between initialization
new: Suitable for creating pointers , Generally, create basic types ,int etc. , The return is the pointer
make: Mainly create reference types , Like slicing ,map. What is returned is the reference of the created type itself , Modification will affect itself , Be similar to php Of &
make Be similar to []int{} To initialize the .
Tell the truth , A little cumbersome , In fact, the key is to initialize this structure , Just quote , This does not return nil, The rest is naturally easy to operate ..
end
边栏推荐
- Event capture and bubbling - what is the difference between them?
- Why can cross entropy loss be used to characterize loss
- Watermelon book learning notes - Chapter 1 and 2
- 基于SSM学生成绩管理系统
- Express receive request parameters
- Significance of NVIDIA SMI parameters
- Watermelon book learning notes - Chapter 4 decision tree
- Reflection on pytorch back propagation
- MySQL的基本语句(1)—增删改查
- PNA modified polypeptide arms PNA PNA DNA suc aapf PNA suc - (ALA) 3 PNA
猜你喜欢

齐岳:巯基修饰寡聚DNA|DNA修饰CdTe/CdS核壳量子点|DNA偶联砷化铟InAs量子点InAs-DNA QDs

【11】 Binary code: "holding two roller handcuffs, crying out for hot hot hot"?

Li Hongyi 2020 deep learning and human language processing dlhlp core resolution-p21

最新!国资委发布国有企业数字化转型新举措

DNA (deoxyribonucleic acid) supply | carbon nanotube nucleic acid loaded dna/rna material | dna/rna nucleic acid modified magnetic nanoparticles

Dsgan degenerate network

MySQL的基本语句(1)—增删改查

C语言怎么学?这篇文章给你完整答案

Analysis of strong tennis cup 2021 PWN competition -- babypwn
![AI: play games in your spare time - earn it a small goal - [Alibaba security × ICDM 2022] large scale e-commerce map of risk commodity inspection competition](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
AI: play games in your spare time - earn it a small goal - [Alibaba security × ICDM 2022] large scale e-commerce map of risk commodity inspection competition
随机推荐
Analysis of online and offline integration mode of o2o E-commerce
Deepsort工作原理分析
CASS11.0.0.4 for AutoCAD2010-2023免狗使用方法
Basic concepts of program, process, thread, coprocess, single thread and multi thread
Visual horizontal topic bug1:filenotfounderror: could not find module 'mvcameracontrol dll‘ (or one of it
DNA modified near infrared two region GaAs quantum dots | GaAs DNA QDs | DNA modified GaAs quantum dots
【Latex格式】双栏双图左右并排有小标题、上下并列有小标题
Interpretation of deepsort source code (II)
Reasoning speed of model
Error in running code: libboost_ filesystem.so.1.58.0: cannot open shared object file: No such file or directory
Watermelon book learning notes - Chapter 1 and 2
DNA research experiment application | cyclodextrin modified nucleic acid cd-rna/dna | cyclodextrin nucleic acid probe / quantum dot nucleic acid probe
Student status management system based on SSM
C time related operation
regular expression
含有偶氮苯单体的肽核酸寡聚体(NH2-TNT4,N-PNAs)齐岳生物定制
Bert and RESNET can also be trained on mobile phones?!
关于卡尔曼滤波的协方差如何影响deepsort的跟踪效果的考虑
Day012 application of one-dimensional array
Leetcode series (I): buying and selling stocks