This is the fifth day of my participation in Gwen Challenge

The structure of the body

Definition and initialization

// Define the structure
type structName struct{
 fieldName typeName
 fieldName2 typeName
 ...
}
Copy the code
  • Use type + struct to declare a struct, which is a type
  • The interface body contains fields, which can be zero, one, or more
// Define the Person structure
type Person struct {
	name string 
	age int
}

// Initialize variables
var p Person  // the variable p defaults to zero
var p2 Person {"jasen".18}  // initialize variable p with field name=jasen, age=18
var p3 Person {age:18,name:"jasen"} // Field names can be unordered
p4 := Person{"Tom".20}
Copy the code

Declare a structure variable p of type Person, where all fields in P default to zero.

A combination of structures

type student struct{
	name string
	age int
}

type group struct {
	name string
	code int
  student   // Insert student structure
}
Copy the code
  • External types can use fields and methods of internal types
  • If the outer type defines the same method as the inner type, the outer type overrides the inner type (a method copy).

interface

Interface definition and implementation

An interface is a specification and abstraction of a class of behavior.

// Define the interface
type interfaceName interface{
  method() result
}

// Interface definition and implementation
type Caller interface{
  sayHi() string
}

type person struct{
  name string
}

The person type implements the interface
funcp *person)sayHi(a) string {
  fmt.Println("Implement interface p.name=",p.name)
  return "ok"
}

Copy the code
  • Use the keyword type + interface to define the interface
  • Method () is a method in the interface, which is the convention of the interface. How to implement it depends on the specific type
  • If an interface has more than one method, a type must implement each method

An interface

// Take the interface as an argument to the function
func printResult(c Caller){
  fmt.Println(sayHi())
}

// Teacher implements the Caller interface as well
type teacher struct{
  name string
}

The person type implements the interface
funct *teacher)sayHi(a) string {
  fmt.Println("Implement interface t.name. =",t.name)
  return "ok"
}

// Call printResult
printResult(p) // Output: ok
printResult(t) // Output: ok
Copy the code

Value receiver and pointer receiver

funct teacher)sayHi(a) string {
  fmt.Println("Implement interface t.name. =",t.name)
  return "ok"
}

funct *teacher)sayHi(a) string {
  fmt.Println("Implement interface t.name. =",t.name)
  return "ok"
}
Copy the code

When a method is bound to a type, the method can be called by either the bound value type or the pointer type. However, there is a difference when implementing interfaces

  • Value type receiver: When implementing an interface, both the corresponding value type and pointer type implement the interface
  • Pointer type receiver: When implementing an interface, only pointer types implement the interface

Inheritance and composition

Go language, there is no concept of inheritance, no parent-child relationship, but through the way of composition, code reuse.

type read interface{... }type write interface{... }// Combine the read and write interfaces. The readAwrite interface has all the methods of both interfaces
type readAwrite interface{
  read
  write
}
Copy the code

Types of assertions

If two types implement the same interface, there is a type assertion

// In the example above, both person and teacher implement the interface Caller
p1 := p.(person)
t1 := t.(teacher)

// The program will throw an error if the assertion fails, and the program will fail to run, so go provides the return value as follows
// Determine the success of the assertion with the OK flag
p1,ok := p.(person)


Copy the code