Object-oriented structure of the body 2

Create a variable structure and access structure fields

1) 1- embodiment declaratively

case presentations: var the Person Person

2)] 2- {}

Case presentation: the Person Person var = {} the Person

FUNC main () {
  P2: = {} the Person
  p2.Name = "Tom"
  p2.Age = 18 is
  fmt.Println (P2)
}


3) 3- & way

案例:var person *Person = new(Person)

main FUNC () {
  // embodiment. 3
  var p3 * = the Person new new (the Person)
  // Since p3 is a pointer, and therefore the standard assignments of fields to
  //(*p3).Name = "smith" may be written = p3.Name "smith"
  // because designers go easy for programmers to use will be processed at the bottom = p3.Name "smith"
  // will add value p3 operation (* p3) .Name = "Smith"
  (* P3) .Name = "Smith"
  p3.Name = "John"
  (P3 *) = 30 .Age
  p3.Age = 100
  fmt.Println (* P3)
}

4) 4- {manner}

案例:var person *Person = &Person{}

main FUNC () {
  // embodiment 4
  // The following statements can also be directly assigned to the character
  @ * the Person person var = & {the Person "Mary", 60}
  var person the Person * = {} & the Person
  // because a person is pointer, so the standard method of accessing field
  // (* the Person) .Name = "scott"
  // Go designers to programmers and easy to use, can also be directly written = person.Name "scott"
  // same as above reasons , the bottom will do some processing
  (Person *) .Name = "Scott"
  person.Name = "Scott ~"
  (Person *) = 88 .Age
  person.Age = 10
  fmt.Println (Person *)
}


Note:
(1) a third and fourth embodiment is the structure pointer returned

(2) a standard way to access pointer field structure should be: (* structure pointer) field names, such as:. (* Person) .Name = "tom"

(3) but it does go a simplified, but also a support structure pointer field names, such as person.Name = "tom". More in line with customary programmers, go to the bottom of the compiler do person.Name conversion (* person) .Name

 

Structure of the memory allocation mechanism: 

Basic instructions:

Variables are always present in memory, then the structure variable exactly how to exist in memory?

Look at the following code and analyze the reasons:

The following piece of code, what information will be output:

var p1 Person
p1.Age = 10
p1.Name = "小明"
var p2 *Person = &p1

fmt.Println((*p2).Age) //p1的Age 10
fmt.Println(p2.Age) //p1的Age 10
p2.Name = "tom~"
fmt.Printf("p2.Name=%v p1.Name=%v \n", p2.Name, p1.Name) //tom~
fmt.Printf("p2.Name=%v p1.Name=%v \n", (*p2).Name, p1.Name) //tom~

Output results:

A schematic view in memory:

Look at the following code, and analyze the reasons

var p1 Person
p1.Age =10
p1.Name = "小明"
var p2 *Person = &p1

fmt.Println (* p2.Age) // Can you write?

No, because after. Operator precedence than * high, so it will be considered first count *. It must be bracketed (* p2) .Age

 

Details structure:

1) All the fields are continuous structures in memory

Case:

type Rect struct {
  leftUp, rightDown Point
}

type Rect2 struct {
  leftUp, rightDown *Point
}

func main() {

  R1: = Rect Point {{1,2}, {3,4- Point}}
  // R1 has four integer int, is continuously distributed in memory
  // print the address
  fmt.Printf ( "r1.leftUp.x address =% p r1.leftUp.y address =% p r1.rightDown.x address address =% p r1.rightDown.y% P = \ n-",
  & r1.leftUp.x, & r1.leftUp.y, R1 & .rightDown.x, & r1.rightDown.y)

  There are two // R2 * Point type, the two types of * Point address itself is also continuous
  // they point to address is not necessarily contiguous
  r2: = Rect2 {& Point { 10,20}, & Point {30, 40}}

  // print the address
  fmt.Printf ( "r2.leftUp own address address =% p r2.rightDown itself% P = \ n-",
  & r2.leftUp, & r2.rightDown)
  // address does not point to their continuous ... this depends on how the system is in operation assigned
  fmt.Printf ( "r2.leftUp point to address =% p r2.rightDown point to address the p-% = \ the n-",
  r2.leftUp, r2.rightDown)
}

A schematic view in memory:

2) a separate structure is a user-defined type, requires identical fields (name, number and type, and when other types of conversion)


type A struct {
  Num int
}

type B struct {
  Num int
}

func main() {

  A A var
  var B B
  A A = (B) // can be converted, but there is required, the field to be exactly the same structure (name, number and type)
  fmt.Println (A, B)

}


3) type structure for redefining (corresponding alias), golang considered a new data type, but can be mutually transferred strong

type Student struct {
  Name string
  Age int
}

type Stu Student

  main FUNC () {
  var STU1 Student
  var STU2 Stu
  STU2 = STU1 // correct? The error may be modified such Stu = STU2 (STU1)
  fmt.Println (STU1, STU2)
}

type integer int

  main FUNC () {
  var Integer I = 10
  var int J = 20 is
  J = I // correct? Wrong because golang integer that is a new data type, and can not be directly assigned to be converted int = J (I)
  fmt.Println (I, J)
}


4) on each of the fields of the struct can write a tag, the tag can be obtained by reflection, that is, common usage scenarios and deserialize serial number.


Case presentation:

package main
import (
  "fmt"
  "encoding/json"
)

type Monster struct {
  Name string `json:"name"` // `json:"name"` 就是 struct tag
  Age int `json:"age"`
  Skill string `json:"skill"`
}

func main() {

  1 // Create a variable Monster.
  Monster: Monster = { "cow devil", 500 "plantain ~"}

  // Variables 2 monster into json format string
  // json.Marshal function using reflection, and then reflected detail when explaining this
  jsonStr, ERR: = json.Marshal (monster)
  ! IF ERR = nil {
    FMT .Println ( "json processing error", ERR)
  }
  fmt.Println ( "jsonStr", String (jsonStr))
}

 

Guess you like

Origin www.cnblogs.com/green-frog-2019/p/11408097.html