Akik Look at that coder
Public account: Look at that code farmer
The last installment introduced ten classic numerical operations for Go language learning | Go Theme month
- 1. Find the sum and average of an array
- 2. Find the maximum value of an array and output the corresponding subscripts of the elements
- 3. Use Go to print the number and sum of multiples of 9 between 1 and 100
- 4. Print an odd number between 1 and 30
- 5. Print an even number between 1 and 30
- 6. Seek 1 + 2! + 3! +… + 20! And of the
- 7. Find the Fibonacci number of the NTH number
- 8. Find the greatest common divisor and least common multiple of two numbers
- Find the number of daffodils within 1000
- 10. Round up, round down, round off
This article will continue to take you into the world of Go.
1. Introduction to this paper
Create interface for Go language learning
2. What is an interface
Interface is a cooperation agreement agreed by both parties. Implementers of interfaces need not care about how the interface will be used, and callers need not care about implementation details of the interface.
The interface of the Go language is a collection of methods that can be understood as abstract types. Any type that implements the set of methods in that interface belongs to that interface.
Interfaces do not expose the contained data layout and internal structure (format, type, structure). Interfaces only provide methods; developers only know what the interface can do.
3. Interface declaration format
Each interface consists of several methods, defined in the following format:
Type Name of the interface Type interface{method1(Parameter list) Returns a list of values1methods2(Parameter list) Returns a list of values2... }Copy the code
Here is our example of creating an interface with code that has only one method:
package main
import (
"fmt"
)
// Define an interface
type People interface {
ReturnName() string
}
// Define a structure
type Student struct {
Name string
}
// A method to define a structure.
Student implements the People interface; // We find a set of methods for the People interface
func (s Student) ReturnName() string {
return s.Name
}
func main() {
node := Student{Name:"Look at that coder."}
var a People
// Because Students implement the interface, direct assignment is ok
a = node
name := a.ReturnName()
fmt.Println(name)
}
Copy the code
The output is:
4. Conditions for implementing interfaces
An object implements the interface as long as it implements all of the methods in the interface.
In other words, an interface is a list of methods that need to be implemented.
Let’s look at the implementation process in detail:
First, let’s define a Sayer interface:
/ / Sayer interface
type Sayer interface {
say()
}
Copy the code
Next, define the dog and cat constructs:
type dog struct {}
type cat struct {}
Copy the code
The Sayer interface has only one SAY method, so we only need to implement say methods for Dog and cat to implement Sayer interface.
// Dog implements the Sayer interface
func (d dog) say() {
fmt.Println("The little dog goes woof woof woof.")}// cat implements Sayer interface
func (c cat) say() {
fmt.Println("Kitty says meow, meow, meow.")}Copy the code
As long as all the methods in the interface are implemented, the interface is implemented.
An interface type variable can store all instances that implement the interface.
For example, in the example above, variables of type Sayer can store variables of type dog and cat.
func main() {
var x Sayer // declare a variable x of type Sayer
a := cat{} // instantiate a cat
b := dog{} // instantiate a dog
x = a // We can assign the cat instance directly to x
x.say() / / meow meow meow
x = b // We can assign dog instances directly to x
x.say() / / auf
}
Copy the code
The complete code is as follows:
package main
import (
"fmt"
)
type Sayer interface {
say()
}
type dog struct {}
type cat struct {}
// Dog implements the Sayer interface
func (d dog) say() {
fmt.Println("The little dog goes woof woof woof.")}Cat implements the Sayer interface
func (c cat) say() {
fmt.Println("Kitty says meow, meow, meow.")
}
func main() {
var x Sayer // declare a variable x of type Sayer
a := cat{} // instantiate a cat
b := dog{} // instantiate a dog
x = a // We can assign the cat instance directly to x
x.say() / / meow meow meow
x = b // We can assign dog instances directly to x
x.say() / / auf
}
Copy the code
The output is:
How about creating two methods in one interface?
Let’s look at the following example:
package main
import (
"fmt"
)
// Define an interface that contains two methods
type People interface {
ReturnName() string
ReturnNumber() string
}
// Define a structure
type Student struct {
Name string
Number string
}
func (s Student) ReturnName() string {
return s.Name
}
func (s Student) ReturnNumber() string {
return s.Number
}
func main() {
node := Student{Name:"Look at that coder.".Number: "158168"}
var a People
a = node
name := a.ReturnName()
number:=a.ReturnNumber()
fmt.Println(name)
fmt.Println(number)
}
Copy the code
The output is:
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.