当前位置:网站首页>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语言AirPassengers数据集可视化
- From perceptron to transformer, a brief history of deep learning
- Nestjs 集成 config module 与 nacos 实现配置化统一
- A Dynamic Near-Optimal Algorithm for Online Linear Programming
- 慕课5、服务发现-Nacos
- Gradle Build Cache引发的Task缓存编译问题
- 真正的缓存之王Caffine Cache
- 迅睿CMS 自定义数据接口-php执行文件代码
- Kotlin1.6.20 new features context receivers tips
- Cross domain cors/options
猜你喜欢

【513. 找树左下角的值】

Visualization of R language penguins dataset

一张图解码 OpenCloudOS 社区开放日

He was in '98. I can't play with him
![[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.](/img/da/9ced1c0a9c386bc8da75dddaa443e5.png)
[resolved] -go_ out: protoc-gen-go: Plugin failed with status code 1.

The road to systematic construction of geek planet business monitoring and alarm system

Visualization of wine datasets in R language

采用QTest进行数据集测试-性能测试-GUI测试

R language organdata dataset visualization

Teach you how to create SSM project structure in idea
随机推荐
R 语言 wine 数据集可视化
84-我对网传<52 条 SQL 语句性能优化策略>的一些看法
迅睿CMS 自定义数据接口-php执行文件代码
87-with as写法的5种用途
R语言AirPassengers数据集可视化
Golang学习笔记—结构体
91-oracle普通表改分区表的几种方法
Cross domain cors/options
怎样实现网页端im即时通讯中的@人功能
他98年的,我玩不过他...
JWT简介
用RNN & CNN进行情感分析 - PyTorch
Ribbon load balancing
Easyclick fixed status log window
MySQL高级(二)
MySQL中如何计算同比和环比
一张图解码 OpenCloudOS 社区开放日
Code to Image Converter | 代码生成漂亮图片工具
模拟串口UART的实现
Dynamicdatabasesource, which supports the master-slave database on the application side