Do you know the structure of go? Hey

structural morphology Before the structure, l...
structural morphology
Initialization of Structures
Method
Value Receiver and Pointer Receiver
When will pointer type recipients be used?
So long, let's set up a student management system
Anonymous Structures
Serialization: Convert structure to json
Deserialize

structural morphology

Before the structure, let's talk about data types, where we have basic types, but we can also customize types and alias them.

  • Both can be set using type, as follows

  • You can clearly see that Ha, adding an equal sign means setting an alias, or adding a custom type Ha.
  • So we also use type for our structure, plus struct, which is almost as good as in c/c++.
type Type name struct { Field name Field type Field name Field type . . . . }
  • Just like a class in java, for example

  • You can also use anonymous structures, which are defined temporarily.

Initialization of Structures

package main import "fmt" // Type of person type person struct { name string age int gender string hobby []string } func main() { var sb person sb.name = "Sao Bao" sb.age = 18 sb.gender = "male" sb.hobby = []string{"Play basketball", "See your sister"} fmt.Printf("%#v", sb) fmt.Println() var kk = person{ name: "Family", age: 14, gender: "female", } fmt.Println(kk) as := person{ "loser like me", 12, "male", []string{"Play basketball"}, } fmt.Println(as) }
  • There are three ways
  • First, by declaring variables, by passing in values
  • Second, enter values directly as key/value
  • Third, input values, like the constructors for all attributes, in order

Method

  • The method inside go is different from that in java. The method inside go is actually a function, but requires that the structure of the call be restricted
  • The following:

func main() { ans := newDog("Sao Bao", 18) ans.wang() }
  • In this way, is it obvious that we can call it in the form of variable. wang(), which is the same as in java, Ah-Ha-Ha-Ha-Ha-Ha-Ha-Ha

In go neha, if the initials of the identifier are capitalized, it means visible to the external package (exposed, public)

  • The above (d dog) is the recipient, which specifies the object to be called
  • Recipients are also divided into, value acceptor, and pointer acceptor

Value Receiver and Pointer Receiver

  • The picture above explains because I know, let's not say you don't look at the code, I won't look at it either. Ah-ha-ha-ha-ha

  • As you can see, he was 18 when he first started
  • What about the receiver of the call value? Age hasn't changed.
  • But when you call the pointer receiver, the age changes
  • This is because, go is all value transfer, copy and paste form, the meaning of CV, value transfer to modify, change is the copy of the past, not the original data, so it will not change,
  • But the pointer points directly to the original data.

When will pointer type recipients be used?

1. The value inside the receiver needs to be modified
2. Receiver is a large object with large copy size
3. Ensure consistency. If one method uses pointer receiver, the other methods also use pointer receiver

So long, let's set up a student management system

package main import ( "fmt" "os" ) // Student Class type student struct { id int64 name string } // constructor func newStudent(id int64, name string) *student { return &student{ id, name, } } // Set up a container for students var ( allStu map[int64]*student ) // Show all students func allStudent() { for k, v := range allStu { fmt.Printf("School Number:%d Full name:%s\n", k, v.name) } } // Add a student func addStudent() { var ( id int64 name string ) fmt.Print("Please enter your student number:") fmt.Scanln(&id) fmt.Print("Please enter your name:") fmt.Scanln(&name) new := newStudent(id, name) allStu[id] = new } // Delete a student func delStudent() { fmt.Print("Enter the number you want to delete:") var ( id int64 ) fmt.Scanln(&id) delete(allStu, id) } func main() { allStu = make(map[int64]*student, 50) for { fmt.Println("Welcome to the Student Management System!") fmt.Println(` 1.View all student information 2.Add a student 3.Delete a student 4.Exit System `) fmt.Print("Ask you what function you want to run! Please enter the specified number:") var selectID int64 fmt.Scanln(&selectID) switch selectID { case 1: allStudent() case 2: addStudent() case 3: delStudent() case 4: os.Exit(1) default: fmt.Println("Without this option, press 4 to exit") } } }
  • If it weren't for the long code, I would have pictured it. Ah-ha-ha-ha
  • Of course, this is just a function version. We've learned how to do it, and of course we can rewrite it to a method version.

Anonymous Structures

  • This is rarely used, mostly within anonymous nested structures.
  • When two structures have duplicate fields, we can extract them
  • For example, there are provinces and cities in human beings, and provinces and cities in the corporate category.
type address struct { province string city string } type person struct { name string age int address } type company struct { name string address }
  • First three structures, region wrapped in address

  • The result is the same, just so convenient.

Serialization: Convert structure to json

  • json is now the most popular format for data transfer, and of course it will
  • Directly above

  • Notable are two points.
  • First, the first letter must be capitalized, and as mentioned earlier, only capitalization can expose an external package to be invoked.
  • Second point: json must continue to write all lower cases after he has finished. Here you can use the.. symbol, the east to the left of the exclamation mark. Then declare an alias that has been transferred. It can also set the alias of the database and the profile.

Deserialize

  • Then let's look at deserialization
// Deserialize s := `{"name":"Little Aggi","age":17}` var p2 person json.Unmarshal([]byte(s), &p2) fmt.Printf("%#v\n", p2) // Output Effect: main.person
  • There's nothing wrong with this. Just pay attention to one place.
  • When passing values, it is necessary to transfer the pointer, otherwise the value of the copy will be modified. The original data will not change. Pointer to Pointer to Pointer to Pointer

Get here today and continue in the evening.

21 October 2021, 12:36 | Views: 6053

Add new comment

For adding a comment, please log in
or create account

0 comments