当前位置:网站首页>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 :
边栏推荐
猜你喜欢
Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition
【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
技术干货|关于AI Architecture未来的一些思考
【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
TCP cumulative acknowledgement and window value update
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211
Use of generics
项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass
Qtip2 solves the problem of too many texts
技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
随机推荐
TCP cumulative acknowledgement and window value update
Inverted chain disk storage in Lucene (pfordelta)
Topic | synchronous asynchronous
Traversal in Lucene
Common architectures of IO streams
PAT甲级 1030 Travel Plan
【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
Qtip2 solves the problem of too many texts
Longest common prefix and
Application of pigeon nest principle in Lucene minshouldmatchsumscorer
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
Jeecg menu path display problem
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
技术干货|昇思MindSpore Lite1.5 特性发布,带来全新端侧AI体验
Comparison of advantages and disadvantages between most complete SQL and NoSQL
【MySQL 12】MySQL 8.0.18 重新初始化
PgSQL converts string to double type (to_number())
昇思MindSpore再升级,深度科学计算的极致创新