Go language structures and methods

struct of use:

1 used to customize the complex data structure

2. struct which may comprise a plurality of fields (attributes)

3. struct type can be defined to distinguish between methods, attention and functions

4. struct type is a value type

5. struct type can be nested

6. Go language is not class type, only type struct

struct declaration:

type identifier {struct 
       field1 type 
       Field2 type 
} 

example: 
type struct {Student 
       the Name String 
       Age int 
Score int 
}

 struct field visit in: and other languages, the point of use

var stu Student

stu.Name = “tony”
stu.Age = 18
stu.Score=20

fmt.Printf(“name=%s age=%d score=%d”, stu.Name, stu.Age, stu.Score)

 struct defined three forms:

was stu Student 

was stu * Student = new (Student) 

was stu * Student = & Student {}

Wherein b and c are returned pointer to a structure, the following form of access

stu.Name, stu.Age and stu.Score or (* stu) .Name, (* stu) .Age like

 struct memory layout: struct all fields in the memory are continuous, the following layout:

struct list:

List definitions:

type Student struct {
       Name string
       Next   *Student
}

Each node contains the address of the next node, so that all the nodes of the string together, usually the first node in the list is called the list head

  

  

  

  

Guess you like

Origin www.cnblogs.com/weidaijie/p/11447961.html