当前位置:网站首页>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}
原网站

版权声明
本文为[Sentiment.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221925394280.html