当前位置:网站首页>Go language learning notes - structure

Go language learning notes - structure

2022-07-07 12:59:00 Bigbear

Go Language structure


1、 Concept
A structure is an aggregate data type , Is an entity composed of zero or more values of any type .
Each value is called a member of the structure .
Go Arrays can store the same type of data in a language , But in the structure, we can define different data types for different items .
A struct is a data set consisting of a series of data of the same or different types .
The data that make up the structure type is called Field (fields). Each field has a type and a name ;
In a structure , Field names must be unique .

2、 characteristic
The concept of structure is called in the old term of software engineering ADT( Abstract data types :Abstract Data Type),
In some old programming languages, it is called Record (Record), such as Cobol, stay C It also exists in family programming languages ,
And the name is struct, In an object-oriented programming language , Like a lightweight class without methods .

3、 Defining structure
type identifier struct {
    field1 type1
    field2 type2
    ...
}


4、 A very simple example :

 

 

5、 Access structure members
If you want to access structure members , You need to use a dot . The operator , The format is : Structure . Member name "

 

 

6、 Initialize structure
1)、 Use new
Use new Function to allocate memory for a new struct variable , It returns a pointer to allocated memory :
var t *T = new(T), If necessary, put this statement on a different line
( For example, the definition is package wide , But distribution doesn't have to be done in the beginning ).
var t *T t = new(T)
The usual way to write this sentence is :t := new(T), Variable t It's a point T The pointer to ,
In this case, the value of the structure field is the zero value of the type they belong to .
Statement var t T I will t Allocate memory , And zero the memory , But at this point t It's the type T.
In both ways ,t It is often called type T An example of (instance) Or object (Object).

2)、 Initialize a structure instance ( A structure literal :struct-literal) The shorter and more commonly used ways of :
ms := &struct1{10, 15.5, "Chris"} // here ms The type is *struct1

3)、 General initialization
 var ms struct1 ms := struct1{10, 15.5, "Chris"}

7、 Structure pointer
You can define pointers to structures, similar to other pointer variables , The format is as follows :
var struct_pointer *Books
The pointer variable defined above can store the address of the structure variable . View the structure variable address ,
Can be & The symbol is placed in front of the structure variable :
struct_pointer = &Book1
Use structure pointers to access structure members , Use "." The operator :struct_pointer.title

 

 

8、 Use the factory method to create a structure instance

Structure factory
Go Language does not support constructor methods like those in object-oriented programming languages ,
But it can be easily in Go To realize “ Construct sub factory “ Method .
For convenience, a factory is usually defined for the type , The customary , The name of the factory begins with new or New start .
Suppose the following File Type of structure :

type File struct {
    fd      int     // File descriptor
    name    string  // file name
}

The following is the factory method corresponding to this structure type ,
It returns a pointer to the instance of the structure :

 

func NewFile(fd int, name string) *File {
    if fd < 0 {
        return nil
    }
    return &File(fd, name)
}

Then call it like this :
f := NewFile(10, "./test.txt")

1、 stay Go Languages often use initialization in factory methods like the above to easily implement constructors .

2、 If File It's a structural type , So the expression new(File) and &File{} It is equivalent. .

3、 This can be compared with the clumsy initialization methods in most object-oriented programming languages :File f = new File(...).

4、 We can say that the factory instantiates an object of type , Like in class based OO In language .

5、 If you want to know the structure type T How much memory does an instance of , have access to :size := unsafe.Sizeof(T{}).

8、 Structure embedding and anonymous members
Consider a two-dimensional drawing program , Provides a library of various graphics , A rectangle, for example 、
ellipse 、 Star and wheel geometry . Here are the definitions of two of them :
type Circle struct {
    X, Y, Radius int
}

type Wheel struct {
    X, Y, Radius, Spokes int
}
One Circle The type of circle represented contains the standard center of the circle X and Y Coordinate information , And a Radius Represents the radius information .
One Wheel The wheel shape contains Circle Except for all members of a type , Also added Spokes Indicates the number of radial spokes .
We can create a wheel Variable :

var w Wheel
w.X = 8
w.Y = 8
w.Radius = 5
w.Spokes = 20


As the number of geometries in the library increases , We will certainly notice the similarities and repetitions between them ,
So we may separate the same attributes for maintenance purposes :

 

 

 

Struct literals must follow the structure at the time of the shape type declaration ,
So we can only use the following two grammars , They are equivalent to each other :
After this change, the structure type becomes clear , But this modification also makes accessing each member cumbersome :
var w Wheel w.Circle.Center.X = 8 w.Circle.Center.Y = 8 w.Circle.Radius = 5 w.Spokes = 20
Struct literals must follow the structure at the time of the shape type declaration , So we can only use the following two grammars , They are equivalent to each other :

 

  well , Thank you for sharing here today   O(∩_∩)O~

 

More technology sharing ->            

 

原网站

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