当前位置:网站首页>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 :
边栏推荐
- Visit Google homepage to display this page, which cannot be displayed
- 【MySQL 12】MySQL 8.0.18 重新初始化
- Pgadmin 4 v6.11 release, PostgreSQL open source graphical management tool
- PAT甲级 1031 Hello World for U
- Various postures of CS without online line
- Use of other streams
- Responsive MySQL of vertx
- New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
- Some basic operations of reflection
- Vertx's responsive redis client
猜你喜欢
Use of generics
Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
Understanding of class
【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
VMware network mode - bridge, host only, NAT network
Circuit, packet and message exchange
Arduino Serial系列函数 有关print read 的总结
随机推荐
opensips与对方tls sip trunk对接注意事项
Jeecg request URL signature
Vertx multi vertical shared data
Lucene introduces NFA
UA camouflage, get and post in requests carry parameters to obtain JSON format content
技术干货|关于AI Architecture未来的一些思考
Epoll related references
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
Use of other streams
Topic | synchronous asynchronous
Leetcode 198: 打家劫舍
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
Industrial resilience
Lombok -- simplify code
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
Inverted chain disk storage in Lucene (pfordelta)
Spa single page application
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
圖像識別與檢測--筆記
Reconnaissance et détection d'images - Notes