当前位置:网站首页>Golang learning notes - structure
Golang learning notes - structure
2022-06-22 20:54:00 【Sentiment.】
Structure
Go Language has no pure object-oriented concept , They all use structures 、 Function, etc . Some features of face object programming , for example : Inherit 、 Combination and other characteristics
Definition
Mainly struct keyword , The structure is as follows :
type struct_name struct{
member defintion
}
type: Structure definition keyword
struct_name: Structure type name
struct: Structure definition keyword
member definition: Member definition
Initialization of structure
When not initialized , Members are all zero values ,int—>0,float—>0,bool—>false,string—>nil
type test struct {
name bool
id float32
sex string
age int
}
var Sentiment test
fmt.Println(Sentiment)
result :
{false 0 0}
Structure member declaration and assignment
Dot operator assignment
Demo01
package main
import "fmt"
func main() {
type test struct {
name string
id int
sex string
age int
}
var Sentiment test // Statement
Sentiment.id = 1 // The following are all part of point operator assignment
Sentiment.name = "Sentiment"
Sentiment.sex = "man"
Sentiment.age = 18
fmt.Println(Sentiment)
}
result :
{
Sentiment 1 man 18}
Key value pair assignment
In addition, you can assign values by key value
package main
import "fmt"
func main() {
type test struct {
name string
id int
sex string
age int
}
Sentiment := test{
name: "Sentiment",
id: 1,
sex: "man",
age: 18, // notes : Finally, you need to add a comma
}
fmt.Println(Sentiment)
}
Anonymous structure
If the structure is used temporarily , You don't have to name it , Use it directly
demo02
package main
import "fmt"
func main() {
var dog struct {
id int
name string
}
dog.id = 1
dog.name = "Tana"
fmt.Println(dog)
}
result :
{
1 Tana}
Structure pointer
demo03
Structure pointers are similar to ordinary variable pointers
package main
import "fmt"
func main() {
type Person struct {
id int
name string
}
var Sentiment = Person{
1, "Sentiment"}
var p_person = &Sentiment
fmt.Printf("Sentiment:%v\n", Sentiment) // Output structure
fmt.Printf("p_person:%p\n", p_person) // Structure address
fmt.Printf("p_person:%v", p_person) // The value corresponding to the structure address
}
result :
Sentiment:{
1 Sentiment}
p_person:0xc000004078
p_person:&{
1 Sentiment}
new Keyword to create a structure pointer
In addition to the above & Assignment creation outside , Can pass new Key to instantiate the structure , What we get is the address of the structure
package main
import "fmt"
func main() {
type Person struct {
id int
name string
}
var p_person = new(Person)
fmt.Printf("p_person:%T\n", p_person)
}
result :
p_person:*main.Person
Structure pointer member assignment
Structure pointer member , You can also assign values with dot operators
package main
import "fmt"
func main() {
type Person struct {
id int
name string
}
var p_person = new(Person)
p_person.id = 1
p_person.name = "Sentiment"
fmt.Printf("p_person:%v", p_person)
}
result :
p_person:&{
1 Sentiment}
Structure as function parameter
A structure can also be like a variable , Pass as an argument to a function , There are two ways :
- Direct transfer structure : The contents of the external structure are not changed inside the function ( It is equivalent to that the value of a call structure itself will not change )
- Pass structure pointer : Inside the function , Can change the content of the external structure ( Because the pointer is used , So it is an operation on the memory address , The structure changes )
Direct transfer structure
Demo04
package main
import "fmt"
type Person struct {
name string
id int
}
func show(person Person) {
person.id = 1
person.name = "Sentiment"
fmt.Println(person)
}
func main() {
person := Person{
"Tana", 2}
fmt.Printf("person: %v\n", person)
fmt.Println("-------------")
show(person) // call show The value changes after the method
fmt.Println("-------------")
fmt.Printf("person: %v", person) // But only one call was made , Once again, the output person Value , It's still the same
}
result :
person: {
Tana 2}
-------------
{
Sentiment 1}
-------------
person: {
Tana 2}
Pass structure pointer
package main
import "fmt"
type Person struct {
name string
id int
}
//func show(person Person) {
// person.id = 1
// person.name = "Sentiment"
// fmt.Println(person)
//}
func show2(person *Person) {
person.id = 3
person.name = "Mumu"
fmt.Println(person)
}
func main() {
person := Person{
"Tana", 2}
fmt.Printf("person: %v\n", person)
fmt.Println("-------------")
show2(&person) // call show2 Method , The obtained formal parameter is a pointer type
fmt.Println("-------------")
fmt.Printf("person:%v", person) // Because it is a pointer type , Get the memory address , Therefore, the external structure value also changes after the call
}
result :
person: {
Tana 2}
-------------
&{
Mumu 3}
-------------
person:{
Mumu 3}
Nesting of structures
Suppose a person Person Structure , This man also keeps a pet Dog Structure
Dog Structure
type Dog struct {
name string
age int
}
Person1 Structure
type Person1 struct {
dog Dog
name string
id int
}
Demo05
package main
import "fmt"
type Dog struct {
name string
age int
}
type Person1 struct {
dog Dog
name string
id int
}
func main() {
Sentiment := new(Person1)
Sentiment.id = 1
Sentiment.name = "Sentiment"
Sentiment.dog.name = "Tana"
Sentiment.dog.age = 3
fmt.Println(Sentiment)
}
result :
&{
{
Tana 3} Sentiment 1}
边栏推荐
猜你喜欢

R language CO2 dataset visualization

Easydss problem and solution summary

R 语言 UniversalBank.csv“ 数据分析

Visualization of R language nutrient dataset

Security policy and NAT (easy IP) of firewall Foundation

He was in '98. I can't play with him

Multi transactions in redis

已解决:一個錶中可以有多個自增列嗎

R 语言 wine 数据集可视化

【Proteus仿真】74LS138译码器流水灯
随机推荐
90-最近优化过的几套Oracle数据库回顾
85-这些SQL调优小'技巧',你学废了吗?
Understand the index of like in MySQL
From perceptron to transformer, a brief history of deep learning
什么?你居然不会微信分身
91-oracle普通表改分区表的几种方法
How to calculate yoy and mom in MySQL
【Proteus仿真】8x8Led点阵数字循环显示
One picture decoding opencloudos community open day
Introduction of neural networks for Intelligent Computing (Hopfield network DHNN, CHNN)
R 语言nutrient数据集的可视化
【已解决】--go_out: protoc-gen-go: Plugin failed with status code 1.
【深入理解TcaplusDB技术】单据受理之建表审批
uniapp小程序商城开发thinkphp6积分商城、团购、秒杀 封装APP
[in depth understanding of tcapulusdb technology] form creation and approval of document acceptance
AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
【513. 找树左下角的值】
Solutions to Oracle system/ user locking
Unityeditor editor script execution menu
MySQL基础——约束