Drunk when song, geometry of life, such as morning dew, the past bitter
Don’t read the water less, old don’t read in The Three Kingdoms, don’t know why come down such words, young age once loved to watch the three series of related TV shows, movies, especially when the plot development to the inventor taoyuan three sworn blood surge, total felling the big fellow of the world should make such talent, liu bei But the final result not only failed to let Liu Bei win the world, but also his son fell a happy ending. Instead of cao cao although the comparison of character in the drama depicting the cold storage, the later is called a generation of biography, but from the perspective of today from ability or from personal cultural attainments, liu bei and cao cao is not a step, if do not have a chibi war defeat, I do not know the course of history will evolve? Drunk when song, geometry of life, such as morning dew, the past bitter. Although the word is a good word, but the style is slightly lonely, just set off the way of learning programming.
The structure of the body
1. What is a structure?
Definition: A struct is a user-defined type that represents a collection of fields. (Struct variables can also be understood as classes in other languages. Fields are attributes in other classes.)
Struct {attribute1 type attribute2 type... Type Employee struct {firstName string lastName string age int}Copy the code
2. Classification of structures
Structure can be divided into the following categories from the Angle of declaration: named structure, anonymous structure
Type Employee struct {firstName, lastName string age, salary int} emp3 := struct {firstName, salary int} lastName string age, salary int }{ firstName: "Andreah", lastName: "Nikola", age: 31, salary: 5000, }Copy the code
3. Anonymous fields of a structure
Func main() {p := Person{"Naveen", 50} fpt.println (p)}Copy the code
4. Inlaying of the structure
Type Address struct {city, state string } type Person struct { name string age int address Address } func main() { var p Person p.name = "Naveen" p.age = 50 p.address = Address { city: "Chicago", state: "Illinois", } fmt.Println("Name:", p.name) fmt.Println("Age:",p.age) fmt.Println("City:",p.address.city) fmt.Println("State:",p.address.state) }Copy the code
5. The equality of structures
A structure is a value type that can be compared if each of its fields is comparable. Two structs are equal if their corresponding value types are equal
Struct {firstName string lastName string} func main() {name1 := name{"Steve", struct {firstName string lastName string} func main() {name1 := name{"Steve", struct {firstName string lastName string} func main() {name1 := name{"Steve", "Jobs"} name2 := name{"Steve", "Jobs"} if name1 == name2 { fmt.Println("name1 and name2 are equal") } else { fmt.Println("name1 and name2 are not equal") } name3 := name{firstName:"Steve", lastName:"Jobs"} name4 := name{} name4.firstName = "Steve" if name3 == name4 { fmt.Println("name3 and name4 are equal") } else { fmt.Println("name3 and name4 are not equal") } }Copy the code
6. Structure with label
A field in a structure has an optional label in addition to its name and type, which is a string attached to the field that can be a document or other important tag. The contents of the tag cannot be used in normal programming; only the package Reflect retrieves it
// tags field1 bool "An important answer" field2 string "The name of The thing" field3 int "How much there are" }Copy the code
7. Structure value allocation
Type struct1 struct {i1 int f1 float32 STR string} func main() {ms := new(struct1) ms. I1 = 10 ms ms.str= "Chris" fmt.Printf("The int is: %d\n", ms.i1) fmt.Printf("The float is: %f\n", ms.f1) fmt.Printf("The string is: %s\n", ms.str) fmt.Println(ms) }Copy the code
summary
Structure of knowledge in general is very important, if this part of the content is unable to conquer, from the perspective of conscience, my sincere advice or give up the language learning, without a master structure knowledge of programming, in the language learning without any kind of advantage, The subsequent learning of advanced content or framework is basically inseparable from the knowledge of structure. Therefore, structure is a threshold that must be overcome on the road of GO.