当前位置:网站首页>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 :
边栏推荐
- 技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
- 【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
- Common architectures of IO streams
- 技术干货|关于AI Architecture未来的一些思考
- Robots protocol
- An overview of IfM Engage
- What did the DFS phase do
- [set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
- VMware network mode - bridge, host only, NAT network
- Web router of vertx
猜你喜欢

Spa single page application

技术干货|关于AI Architecture未来的一些思考

为什么说数据服务化是下一代数据中台的方向?

Leetcode 213: looting II

Custom generic structure

Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!

项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass

Leetcode 198: 打家劫舍

Margin left: -100% understanding in the Grail layout

Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
随机推荐
The concept of C language pointer
The underlying mechanism of advertising on websites
Common operations of JSP
Wireshark software usage
IO stream system and FileReader, filewriter
【MindSpore论文精讲】AAAI长尾问题中训练技巧的总结
最全SQL与NoSQL优缺点对比
Introduction of transformation flow
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
Various postures of CS without online line
Industrial resilience
技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
FileInputStream and fileoutputstream
IndexSort
Es writing fragment process
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
Spa single page application
Logging log configuration of vertx
Some basic operations of reflection
Circuit, packet and message exchange