当前位置:网站首页>Go language foundation ----- 05 ----- structure
Go language foundation ----- 05 ----- structure
2022-07-03 07:37:00 【Mango sauce】
One Structure related learning
go The structure of language is relatively simple to learn , Let's look at it in order .
package main
import "fmt"
// The declarations of ordinary variables are var keyword , There is no need for var
type Student struct {
id int
name string
sex byte
age int
addr string
}
func main(){
// 1. Initialization of structure
// 1.1 Structure ordinary variable initialization
// 1) Sequence initialization , Each member must be assigned
var s1 Student = Student{
1, "mm", 'm', 11, "gz"}
fmt.Println("s1 = ", s1)
// 2) Initialize members , If there is no assignment, the default value is automatically assigned according to the type
s2 := Student{
id:1, name: "gg"}
fmt.Println("s2 = ", s2)
// 1.2 Structure pointer variable initialization
// 1) Sequence initialization , Each member must be assigned , But we need to add &.
var s3 *Student = &Student{
2, "mm", 'm', 11, "gz"}
fmt.Println("s3 = ", s3)
// 2) Initialize members , If there is no assignment, the default value is automatically assigned according to the type
s4 := &Student{
id:1, name: "hh"}
fmt.Println("s4 = ", s4)
// 2. Use of structural members
// 2.1 Use of common variables
var s5 Student
s5.id = 100
s5.name = "hc"
s5.sex = 'w'
s5.age = 18
s5.addr = "jm"
fmt.Println("s5 = ", s5)
// 2.2 The use of pointer variables
// go Pointer calls in member variables are all points of use (.),p.id and (*p).id It's exactly the same , non-existent C Of ->
var s6 Student
var p *Student
p = &s6
p.id = 11
(*p).name = "lqq"
p.sex = 'w'
p.age = 24
p.addr = "where"
fmt.Println("s6 = ", s6)
// 3. Comparison and assignment of structures
// 3.1 Structure comparison
var s7 Student = Student{
id: 1, name: "q", sex: 'w', age: 12, addr: "aa"}
var s8 Student = Student{
id: 1, name: "q", sex: 'w', age: 12, addr: "aa"}
var s9 Student = Student{
id: 2, name: "q", sex: 'w', age: 12, addr: "aa"}
fmt.Println("s7==s8", s7 == s8)
fmt.Println("s7==s9", s7 == s9)
// 3.2 Structure assignment
var t Student
t = s7
fmt.Println("t = ", t)
fmt.Println("t == s7", t == s7)
// 4. Structure as function parameter
// 1) Structure as function parameter is value transfer .
// 2) Structure pointer as function parameter is address passing . Similar to arrays .
// 5. go The visibility of language
// 5.1 go and C++ Dissimilarity , No, private、protected、public These keywords .
// Its visibility is determined by case , When calling functions of other packages 、 Type of structure 、 Structure member variables ,
// Their initials should be capitalized , Otherwise, an error will be reported .
}
result :
边栏推荐
- Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
- 昇思MindSpore再升级,深度科学计算的极致创新
- Jeecg menu path display problem
- Vertx's responsive redis client
- JS monitors empty objects and empty references
- Implementation of breadth first in aggregation in ES
- 项目经验分享:基于昇思MindSpore实现手写汉字识别
- Industrial resilience
- Beginners use Minio
- 技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务
猜你喜欢

Margin left: -100% understanding in the Grail layout

不出网上线CS的各种姿势
![[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211](/img/55/fea5fe315932b92993d21f861befbe.png)
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211

TCP cumulative acknowledgement and window value update

Topic | synchronous asynchronous

Comparison of advantages and disadvantages between most complete SQL and NoSQL

技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估

The embodiment of generics in inheritance and wildcards

HCIA notes

技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务
随机推荐
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Vertx restful style web router
Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition
Download address collection of various versions of devaexpress
Custom generic structure
Summary of Arduino serial functions related to print read
Introduction of buffer flow
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
为什么说数据服务化是下一代数据中台的方向?
Vertx's responsive MySQL template
List exercises after class
Take you through the whole process and comprehensively understand the software accidents that belong to testing
Why is data service the direction of the next generation data center?
图像识别与检测--笔记
技术干货|关于AI Architecture未来的一些思考
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211
Collector in ES (percentile / base)
The difference between typescript let and VaR
VMware virtual machine installation
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq