当前位置:网站首页>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
边栏推荐
- O2O电商线上线下一体化模式分析
- Digital image processing -- Chapter 3 gray scale transformation and spatial filtering
- deepsort源码解读(三)
- deepsort源码解读(七)
- Express framework
- Analysis of online and offline integration mode of o2o E-commerce
- Li Hongyi 2020 deep learning and human language processing dlhlp core resolution-p21
- Cyclegan parsing
- Peptide nucleic acid oligomer containing azobenzene monomer (nh2-tnt4, n-pnas) Qiyue biological customization
- 硫化镉CdS量子点修饰脱氧核糖核酸DNA|CdS-DNA QDs|近红外CdS量子点偶联DNA规格信息
猜你喜欢

How to make the minimum API bind the array in the query string

Watermelon book learning Chapter 5 --- neural network

Boostrap

Dimension problems and contour lines

Significance of NVIDIA SMI parameters

基于SSM实现的校园新闻发布管理系统

Disk management and file system

脱氧核糖核酸DNA改性近红外二区砷化镓GaAs量子点|GaAs-DNA QDs|DNA修饰GaAs量子点

Image super-resolution evaluation index

How to delete or replace the loading style of easyplayer streaming media player?
随机推荐
How to delete or replace the loading style of easyplayer streaming media player?
Two ways of multi GPU training of pytorch
PNA peptide nucleic acid modified peptide suc Tyr Leu Val PNA | suc ala Pro Phe PNA 11
Matlab drawing (ultra detailed)
Express receive request parameters
基于SSM学生成绩管理系统
Gbase 8C technical features
Talk about multimodality of fire
What is the reason why dragging the timeline is invalid when playing device videos on the easycvr platform?
Digital image processing Chapter 1 Introduction
TS learning (VIII): classes in TS
Gbase 8C - SQL reference 6 SQL syntax (13)
CdS quantum dots modified DNA | CDs DNA QDs | near infrared CdS quantum dots coupled DNA specification information
MySQL的基本语句(1)—增删改查
Cass11.0.0.4 for autocad2010-2023 dog free usage
Analysis of strong tennis cup 2021 PWN competition -- babypwn
工控用Web组态软件比组态软件更高效
关于卡尔曼滤波的协方差如何影响deepsort的跟踪效果的考虑
Dimension problems and contour lines
脱氧核糖核酸DNA改性近红外二区砷化镓GaAs量子点|GaAs-DNA QDs|DNA修饰GaAs量子点