Akik Look at that coder

Public account: Look at that code farmer

The last installment introduced the Go language learning operator | Go theme month

  • Arithmetic operator
  • Relational operator
  • Logical operator
  • An operator
  • The assignment operator
  • Other operators

This article will continue to take you into the world of Go.

1. What is the structure

The Go language forms new types in a custom way. Constructs are compound types with members within a type.

Structs allow developers to reference a series of related values through a single variable.

By using structs, you can store many different types of data fields in a single variable.

Structs provide a flexible way to create data results for data.

2. Structure definition format

The Go keyword type can define a variety of primitive types as custom types, including integers, strings, booleans, and so on.

Struct definition format is as follows:

Type Type name struct{field1field1The type field2field2Type... }Copy the code
  • Type name: Identifies the name of a custom struct. it cannot be repeated within the same package.
  • Struct{} : Struct{} : Struct{} : Struct{} : Struct{} : Struct{};
  • Field 1, Field 2…… : indicates the structure field name. The field name of the structure must be unique
  • Field 1 type, Field 2 type…… : represents the type of the structure field.

1. Create struct instance method 1

For example, in the following case:

package main

import "fmt"

type num struct {
   node1 int
   node2 int
}
func main() {
   m:=num{
      node1: 5.node2: 10,
   }
   fmt.Printf("Node1 value: %v\nnode2 value: %v",m.node1,m.node2)

}
Copy the code

The output is:

2. Create struct instance method 2

In addition to the assignment method in the above example, there is another way to use struct assignment:

package main

import "fmt"

type num struct {
   node1 int
   node2 int
}
func main() {
   var m num
   m.node1=1
   m.node2=2
   fmt.Printf("Node1 value: %v\nnode2 value: %v",m.node1,m.node2)

}
Copy the code

The output is:

3. Create a struct instance

The Go language can also use the keyword new to create struct instances,

As shown in the following case:

package main

import "fmt"

type num struct {
   node1 int
   node2 int
}
func main() {
   m: =new(num)
   m.node1=1
   m.node2=2
   fmt.Printf("Node1 value: %v\nnode2 value: %v",m.node1,m.node2)

}
Copy the code

The output is:

3. Nested structures

Nesting one structure within another is a common way to create complex data structures based on business requirements.

For example, the basic information of every colleague in the company (such as name, age and address) needs to be counted, and the address itself is also a data structure, which often contains the city, block name, house number and other information.

Take this example:

package main

import "fmt"

type People struct {
   Name    string
   Age     int
   Address Address
}

type Address struct {
   City   string
   Street string
   Number int
}

func main() {
   m:=People{
      Name: "Lisan",
      Age:  22,
      Address: Address{
         City:   "Beijing",
         Street: "Central Street",
         Number: 1006,
      },
   }
   fmt.Printf("%+v\n",m)

}
Copy the code

The output is:

4. Compare structures

Structures are compared to see if they have the same type and value.

Structures of the same type can be compared using the equality operator.

  • To determine whether two structures are equal, use “==”
  • To determine if two structures are unequal, use the “! =”

The specific code is shown in the following case:

package main

import "fmt"

type num struct {
   node1     int
   node2     int
}

func main() {

   a:=num{
      node1: 1.node2: 2,}b:=num{
      node1: 1.node2: 2,}if a==b{
      fmt.Println("a == b\n")}else{
      fmt.Println("a ! = b\n")}}Copy the code

The output is:

5. Anonymous structures

The initialization method of anonymous structure consists of structure definition and key-value pair initialization.

It can be used directly without the type keyword definition.

  • Anonymous structure definitionThere is no structure type name, onlyfieldandThe type definition.
  • The key-value pair initialization sectionThe optionalMultiple key-value pairsComposition.

For example, the following format is used to create an instance of an anonymous structure:

ins:=struct{
// Anonymous structure field definitionfield1The field type1field2The field type2... } {// Initialize the field valueInitialization field1Fields:1Initializes the field with the value of2Fields:2The value of the... }Copy the code

In an anonymous structure, the initialization part of the key-value pair is optional, and no member is initialized. The format of the anonymous structure is:

ins:=struct{
// Anonymous structure field definitionfield1The field type1field2The field type2... } {}Copy the code

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.