“This is the 13th day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021”.

1 Type aliases and custom types

1.1 User-defined Types

There are some basic data types in Go, such as string, integer, floating point, Boolean, etc. In Go, you can use the type keyword to define custom types.

A custom type defines an entirely new type. We can define it based on built-in primitive types, or we can define it through structs. Such as:

// define MyInt as an int
type MyInt int
Copy the code

By definition of the type keyword, MyInt is a new type that has the properties of int

1.2 Type Aliases

Type aliases are a new feature added in the Go1.9 release.

Type aliasing states that TypeAlias is simply an alias of Type, and TypeAlias is essentially the same Type as Type. For example, when a child has a nickname, a baby name, after school with a scientific name, The English teacher will give him An English name, but these names refer to him.

 type TypeAlias = Type
Copy the code

Rune and byte are type aliases we’ve seen before. They are defined as follows:

 type byte = uint8
 type rune = int32
Copy the code

1.3 Differences between a type definition and a type alias

The following code illustrates the apparent difference between a type alias and a type definition with just an equal sign.

package main
 import (
 	"fmt"
 	"reflect"
 )
 // Type definition
 type NewInt int
 // Type alias
 type MyInt = int
 func main(a) {
 	var a NewInt
 	var b MyInt
 	fmt.Printf("Variable A is of type :%T\n", a) // The type of variable A is main.newint
 	fmt.Printf("The type of variable B is :%T\n", b) // The type of variable b is :int
 	// Check whether a and B are of the same type
 	fmt.Println(reflect.TypeOf(a).Kind() == reflect.TypeOf(b).Kind()) //true
 }
Copy the code

The result shows that the type of a is main.NewInt, which represents the NewInt type defined under the main package. B is of type int. The MyInt type only exists in the code, and does not exist when the compilation is complete

2 structure

: : : Tip overview of the language of the basic data type can represent some of the basic properties of things, but when we want to say a thing of all or part of the property, this time with a single basic data type obviously couldn’t meet the demand, the language provides a custom data types, multiple basic data types can be encapsulated, This data type is called struct. So we can define our own type from structs. Similar to class :: in Java

2.1 Structure definition syntax

Use the type and struct keywords to define a structure in the following format:

typeType namestructField Name Field Type Field name Field type...... }Copy the code

: : : danger notice

  • Type name: Identifies the name of a custom structure that cannot be repeated within the same package.
  • Field name: Indicates the structure field name. The field names in the structure must be unique.
  • Field type: Represents the specific type of a structure field.

: : :

2.2 Three forms of structure definition

The first [basic instantiation]

var stu Student
Copy the code

Second type [pointer type structure]

var stu *Student = new(Student)
Copy the code

3. Take the address of the structure to instantiate, through the & operation

var stu *Student = &Student{}
Copy the code

2.3 Structure instantiation

For example, the following

/ / student
type Student struct {
  Name string   / / name
  Age int       / / age
  Sex bool      // Gender true: male false: female
}
Copy the code

2.3.1 Basic instantiation

// Define a variable stu
var stu Student
// Uninitialized structures whose member variables are zero values corresponding to their types.
fmt.Println(stu) //{ 0 false}
stu.Name="Zhang"    / / assignment
stu.Age=18
stu.Sex=true
fmt.Println(stu) //{{18 true}
fmt.Println(stu.Age)   // Access the field
fmt.Printf("%T",stu)  //main.Student
Copy the code

2.3.2 Initializing the structure with key-value pairs

Key values are separated by:; Key value pairs are separated by a comma

::: tip Key value pair syntax is as follows

Variable name := structure type name {field1Fields:1The value of the field2Fields:2The value of the... }Copy the code

: : :

Example of a key-value pair initializer structure

stu3 := Student{
  Name: "Bill",
  Age: 18}
Copy the code

2.3.3 The value list fills the structure

The syntax for filling a value list structure is as follows ::: tip

Variable name := structure type name {field1The value of the field2The value of the... }Copy the code

The following is an example of a value list filling structure

stu4 := Student{
  "Fifty".18.true,}Copy the code

Anonymous structure

Anonymous structures can also be used in situations such as defining some temporary data structures.

::: tip syntax is as follows

// Note: the anonymous structure is the var declaration variable, and the normal structure is the type outside the function
varType namestructField Name Field Type Field name Field type...... }Copy the code

::: Examples are as follows

// Anonymous structure definition
var user struct{
   name string
   age int
   address string
}
user.name="Fu Xiao Lin"
user.age=29
user.address="Chengdu"
fmt.Println(user) //{fuxiaolin 29 Chengdu}
fmt.Printf("%T\n", user) //struct { name string; age int; address string }
Copy the code

4 Structure pointer

4.1 Creating a pointer type structure

Note that the Go language supports direct use of structure Pointers. To access the members of the structure.

varThe variable name =new(Structure name)Copy the code
package main

import "fmt"
/ / student
type Student struct {
	Name string   / / name
	Age int       / / age
	Sex bool      // Gender true: male false: female
}
func main(a) {
	// Create a body variable of type pointer
var stu1  = new(Student) //*Student
stu1.Name = "Bill" // To access a structure member using a structure pointer, use ". Operators.
stu1.Age = 20 // Equivalent to (*stu1).age =20. Manipulate structure members
stu1.Sex = false
fmt.Println(stu1)
fmt.Printf("%T\n", stu1)     //*main.Student
fmt.Printf("stu1=%v\n", stu1) //stu1=&{false}
fmt.Printf("%p\n", stu1)     //stu1 holds a memory address 0xC0000044c0
fmt.Printf("%p\n", &stu1)     // Stu1 memory address 0xC000006028
}
Copy the code

4.2 Instantiate the address of the structure

Fetching the address of a structure using & is equivalent to a new instantiation of the structure type.

stu3 := &Student{}
fmt.Printf("%T\n", stu3)     //*main.Student
fmt.Printf("stu3=%#v\n", stu3) //stu3=&main.Student{Name:"", Age:0, Sex:false}
stu3.Name="Fu Xiao Lin"
stu3.Age=30
stu3.Sex=true
fmt.Printf("stu3=%v\n", stu3) //stu3=&{stu3 true}
Copy the code

5 struct as function parameter

funcThe function name(struct variable, struct pointer variable){
    / / the function body
}
Copy the code

For example, the following

// Parameters are structure variables
func (stu Student) sayHi(a) {
	fmt.Println(stu.Name, "Hello")}// Arguments are struct pointer variables
func printStudent(stu *Student) {
	fmt.Println(stu.Name, stu.Age, stu.Sex)
}
Copy the code

6 Constructors

Go constructs do not have constructors; we can implement them ourselves. For example, the code below implements a Student constructor. Since struct is a value type, the value copy performance overhead is high if the structure is complex, so this constructor returns the structure pointer type.

/ / student
type Student struct {
	Name string   / / name
	Age int       / / age
	Sex bool      // Gender true: male false: female
}

func newStudent(Name string,Age int,Sex bool) *Student{

	return &Student{
		Name:Name,
		Age: Age,
		Sex:  Sex,
	}
}
func main(a) {
    // Call the constructor to assign
	stu5:=newStudent("Zhang".18.true)
	fmt.Println(stu5)
	fmt.Printf("%p\n", &stu5.Age)
	fmt.Println(stu5)
}
Copy the code

7 The anonymous fields of the structure

A structure allows its member fields to be declared without a field name but only with a type. Such nameless fields are called anonymous fields

//Person structure the Person type
type Person struct {
	string
	int
}

func main(a) {
	p1 := Person{
		"Fu Xiao Lin".18,
	}
	fmt.Printf("%#v\n", p1)        Person{string:" foo ", int:18}
	fmt.Println(p1.string, p1.int) // Fukukobayashi
}
Copy the code

By default, an anonymous field uses the type name as the field name. The structure requires that the field name be unique. Therefore, only one anonymous field of the same type can be created in a structure.

8 Nested structures

A structure can contain another structure or a structure pointer nested within it

package main

import "fmt"

//Address Address structure
type Address struct {
	Province string
	City     string
}

//User User structure
type User struct {
	Name    string
	Gender  string
	Address Address
}

//company structure
type company struct {
	Name    string
	Address Address
}

func main(a) {
	user1 := User{
		Name:   "Liu Qiangdong",
		Gender: "Male",
		Address: Address{
			Province: "Jiangsu",
			City:     "Suqian",
		},
	}
	fmt.Printf("user1=%#v\n", user1)

	company1 := company{
		Name:   "Alibaba",
		Address: Address{
			Province: "Zhejiang",
			City:     "Hangzhou",
		},
	}
	fmt.Printf("company1=%#v\n", company1)
}
Copy the code

9 Nested anonymous structures

package main

import "fmt"

//Address Address structure
type Address struct {
	Province string
	City     string
}

//User User structure
type User struct {
	Name    string
	Gender  string
	Address // Anonymous field
}

//company structure
type company struct {
	Name    string
	Address
}


func main(a) {
	user1 := User{
		Name:   "Liu Qiangdong",
		Gender: "Male",
		Address: Address{
			Province: "Jiangsu",
			City:     "Suqian",
		},
	}
	fmt.Printf("user1=%#v\n", user1)
	// The nested structure accesses attributes
	fmt.Println(user1.Address.City)
	// Short for anonymous nested structure access properties
	fmt.Println(user1.City)
}
Copy the code

When accessing a structure member, the field will be searched in the structure first, and if it cannot be found, it will be searched in the anonymous structure.

10 Field name conflict in nested structure

The same field name may exist inside a nested structure. In this case, you need to specify the fields of the concrete embedded structure to avoid ambiguity.

//Address Address structure
type Address struct {
	Province   string
	City       string
	CreateTime string
}

// the Email structure
type Email struct {
	Account    string
	CreateTime string
}

//User User structure
type User struct {
	Name   string
	Gender string
	Address
	Email
}

func main(a) {
	var user3 User
	user3.Name = "Fu Xiao Lin"
	user3.Gender = "Male"
	// user3.CreateTime = "2019" //ambiguous selector user3.CreateTime
	user3.Address.CreateTime = "2000" // Specify CreateTime in the Address structure
	user3.Email.CreateTime = "2000"   // Specify CreateTime in the Email structure
}
Copy the code

Structure and JSON serialization

JSON(JavaScript Object Notation) is a lightweight data interchange format. Easy to read and write. It is also easy for machine parsing and generation. JSON key-value pairs are a way of storing JS objects. The key/value pair combinations are preceded by the key name and enclosed with double quotation marks (“”), separated by a colon:, followed by the value; Multiple key values are separated in English.

package main
 
import (
	"encoding/json"
	"fmt"
)
 
// Attribute must be capitalized, otherwise json will not parse
type Person struct {
	Name string
	Age int
	Sex string
	Hobby []string
}
 
var wek,alice Person
func init(a) {
	// Initialize two structures
	wek = Person{"wek".18."Male"And []string{"alice"."play"."code"}}
	alice = Person{"alice".17."Female"And []string{"wek"."study"."teacher"}}}func main(a) {
	Serialize the WEK structure
	wekBytes, e := json.Marshal(wek)
	ife! =nil{
		fmt.Println("Structure serialization failed!")}// Prints the WEK serialization structure
	fmt.Println("Serialized result is:",wekBytes)
	fmt.Println("Serialized result converted to string:".string(wekBytes))
	// Serialize the Alice structure
	aliceBytes, e := json.Marshal(alice)
	ife! =nil{
		fmt.Println("Structure serialization failed!")}// Prints the result of Alice serialization
	fmt.Println("Serialized result is:",aliceBytes)
	fmt.Println("Serialized result converted to string:".string(aliceBytes))
}
Copy the code

The output

The serialization result is: [123 34 78 97 109 101 34 58 34 119 101 107 34 44 34 65 103 101 34 58 49 56 44 34 83 101 120 34 58 34 231 148 183 34 44 34 72 111 98 98 121 34 58 91 34 97 108 105 99 101 34 44 34 112 108 97 121 34 44 34 99 111 100 101 34 93 125] the serialized result is converted tostringAfter: {"Name":"wek"."Age":18."Sex":"Male"."Hobby": ["alice"."play"."code"} serialization result is: [123 34 78 97 109 101 34 58 34 97 108 105 99 101 34 44 34 65 103 101 34 58 49 55 44 34 83 101 120 34 58 34 229 165 179 34 44 34 72 111 98 98 121 34 58 91 34 119 101 107 34 44 34 115 116 117 100 121 34 44 34 116 101 97 99 104 101 114 34 93 125] the serialized result is converted tostringAfter: {"Name":"alice"."Age":17."Sex":"Female"."Hobby": ["wek"."study"."teacher"]}
Copy the code

12 Structure Tag

The Tag is the meta information of the structure that can be read at runtime via reflection. The Tag is defined after the structure field and is enclosed by a pair of back quotes in the following format

Summary: : : danger

  • Golang is a non-object-oriented language, and constructs in Go are similar to Classes in Java, but clearly lack OO features such as inheritance polymorphism
  • A pointer variable accesses a structure member through a dot, and must be accessed through * if it’s C or C++, which is an optimization of Go

: : :