当前位置:网站首页>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 :
边栏推荐
- docket
- Sent by mqtt client server of vertx
- JS monitors empty objects and empty references
- Jeecg menu path display problem
- 【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
- Common methods of file class
- Leetcode 198: 打家劫舍
- 技术干货|关于AI Architecture未来的一些思考
- Introduction of buffer flow
- Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
猜你喜欢

Lucene hnsw merge optimization

不出网上线CS的各种姿势

The concept of C language pointer

Collector in ES (percentile / base)

PAT甲级 1029 Median

你开发数据API最快多长时间?我1分钟就足够了

【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)

技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!

VMWare网络模式-桥接,Host-Only,NAT网络

技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
随机推荐
圖像識別與檢測--筆記
Why is data service the direction of the next generation data center?
VMware virtual machine installation
【LeetCode】2. Valid Parentheses·有效的括号
Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition
Leetcode 213: looting II
Rabbit MQ message sending of vertx
Leetcode 198: house raiding
[coppeliasim4.3] C calls UR5 in the remoteapi control scenario
Responsive MySQL of vertx
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model
最全SQL与NoSQL优缺点对比
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq
UA camouflage, get and post in requests carry parameters to obtain JSON format content
JUnit unit test of vertx
Unified handling and interception of exception exceptions of vertx
专题 | 同步 异步
Leetcode 198: 打家劫舍
Understanding of class