This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August

What is a structure

Golang arrays can store the same type of data, but in a structure we can define different data types for different items. A structure is a collection of data with columns of the same or different types of data

Structs are value types used to customize data types and implement object orientation

Functions and application scenarios

Purpose: Used to define custom types

Application scenario: User-defined data types are object-oriented

Structure definition and initialization

type struct_variable_type struct { member definition; member definition; . member definition; }Copy the code

Once a structure type is defined, it can be used to declare variables

variable_name := structure_variable_type { valur1, value2... valuen }Copy the code

Initialize the structure

P := person("Tom", 25) // 2. P := person{age: 24, name: P := new(person) p := new(person) p.age = 24Copy the code

Method of defining a user-defined type 1

type structValue struct {} type integer int type intVariables int type integerVariables integer intVariables = Fmt. Println(intVariables, integerVariables) // how to assign values to each other? IntVariables = int(integerVariables)Copy the code

Define a custom type of way 2 structure

Structure fields can be of any type, as well as the structure itself, functions, interfaces, and so on

A structure type is an abstraction of something concrete

Take the moon cake of Mid-Autumn Festival for example, the template of moon cake, make moon cake

This is done by combining fields of certain data

Struct {field1 type1 field2 type2... }Copy the code

Use of structure 1

type userinfo struct { name string age int height float32 eduschool string hobby []string moreinfo Map [string]interface{}} var afei userinfo afei.name = "afei" afei.age = 22 afei.height = 156 afei.eduschool = Afei. moreinfo = map[string]interface{} {" work": "a robot company ", "duty": "Go Language Engineer"Copy the code

Use of structure 2

:= Short declaration to implement a structure variable

The second form does not have the field name and only declares the corresponding value. Each value can be on a single line, but traditionally this form is written on a single line and does not need a comma at the end. In this form, the order of values is important. It must be the same as the order of the fields in the structure declaration, and all fields in the structure must be assigned values if we do not top the field names

Userinfo := userinfo {name: "afi ", age: 22, height: 156, eduschool:" Wudaokou ", hobby: [] string {" trill ", "code", "daydream"}, moreinfo: map [string] interface {} {" work ":" a robot company ", "duty" : "the language engineer}"Copy the code

Do not specify a field name, need to strictly in accordance with the order of the definition structure when the assignment Specify the field name, can not by definition structure when the order of the assignment Structure assignment, if each field is not in a line, the last field must add a comma Structure assignment, if each field on the same line, The last field can be added without a tickle sign

Xiaoge: the userinfo = {" little brother ", 12120, "primary school", string [] {" learning ", "play", "playing games"}, Interface map [string] {} {" grade ":" grade 6 "}} FMT) Printf (" xiaoge = % v \ n ", xiaoge)Copy the code

Use mode of structure 3 new

Use new new(int),new(string),new(T) to return the structure pointer

//var t * t //t = new(t) var xiaoming * userInfo xiaoming = new(userinfo) (*xiaoming). Name = "xiaoming" (*xiaoming). Age = 12 (*xiaoming ->(*xiaoming ->(*xiaoming ->(*xiaoming ->(*xiaoming ->(*xiaoming ->(*xiaoming ->))); fmt.Println(xiaoming)Copy the code

The way structure is used 4 & address characters

The structure pointer is also returned

Var xiaohong * the userinfo = & the userinfo {" little red ", 12120, "primary school", string [] {" learning ", "play", "playing games"}, map [string] interface {} {" grade ":" grade five "},}Copy the code

Structural matters needing attention

When you do not specify a field name, you need to assign values in the exact order in which the structure was defined

  • Structs are value types
  • Can structures be converted to each other? Can be converted, the prerequisite: have the same field (number, type, name)
  • A structure can be the type of another structure field
  • Struct variable assignment. If the fields are not on the same line, the last field must be comma
  • Struct variable assignment, each field on the same line, the last field comma can be added, or not