Home > Common Problem > body text

What are the abstract methods in Go language?

DDD
Release: 2023-07-13 15:10:18
Original
1258 people have browsed it

Go language abstract methods include interfaces, function types and type combinations. Several methods are introduced in detail: 1. Interface implements abstract methods. Interface is a way of gathering method declarations together. Through interfaces, we can define A set of related methods; 2. Function types implement abstract methods. Function types are a special type that can pass and assign functions as values; 3. Type combinations implement abstract methods. Type combinations are a way to combine multiple The way types are combined into a new type, by nesting the new type with fields or methods of other types, can achieve the effect of abstract methods.

What are the abstract methods in Go language?

#The operating environment of this article: Windows 10 system, go1.20 version, dell g3 computer.

Go language is a modern and efficient programming language with powerful abstraction capabilities. Through abstract methods, common functions can be extracted to make code more reusable. This article will introduce several ways to implement abstract methods in Go language.

1. Interface implementation of abstract methods

In Go language, abstract methods can be implemented through interfaces. An interface is a way of grouping method declarations together, through which a set of related methods can be defined. All methods defined in the interface must be implemented, otherwise a member error will occur.

The following is an example showing how to implement abstract methods through interfaces:

package main
import "fmt"
type shape interface {
area() float64
}
type rectangle struct {
width float64
height float64
}
func (r rectangle) area() float64 {
return r.width * r.height
}
func main() {
r := rectangle{
width: 10,
height: 5,
}
var s shape
s = r
fmt.Println("矩形的面积为:", s.area())
}
Copy after login

In the above example, a `shape` interface is defined, which has only one abstract method `area`. Then a `rectangle` structure is defined and the `area` method is implemented for the structure. In the `main` function, abstract-oriented programming is implemented by assigning `r` to the variable `s` of the `shape` type. Finally, the area of ​​the rectangle is output.

2. Function types implement abstract methods

In addition to interfaces, Go language also supports the use of function types to implement abstract methods. A function type is a special type that allows functions to be passed and assigned as values. This approach can make the code more concise and more flexible.

The following is an example showing how to implement abstract methods through function types:

package main
import "fmt"
type calculator func(float64, float64) float64
func add(a, b float64) float64 {
return a + b
}
func subtract(a, b float64) float64 {
return a - b
}
func main() {
var c calculator
c = add
fmt.Println("1 + 2 = ", c(1, 2))
c = subtract
fmt.Println("1 - 2 = ", c(1, 2))
}
Copy after login

In the above example, a function type `calculator` is defined, which has two `float64` types parameters and a return value of type `float64`. Then two functions `add` and `subtract` are defined to implement addition and subtraction operations respectively. In the `main` function, abstract-oriented programming is implemented by assigning `add` and `subtract` to the variable `c` of the `calculator` type. Finally, the result of 1 2 and the result of 1-2 are output.

3. Type combination to implement abstract methods

In addition to interface and function types, Go language can also implement abstract methods through type combination. Type composition is a way to combine multiple types into a new type. By nesting the new type with fields or methods of other types, the effect of abstract methods can be achieved.

The following is an example showing how to implement abstract methods through type combination:

package main
import "fmt"
type person struct {
name string
age int
}
func (p *person) talk() {
fmt.Println("I am", p.name, "and I am", p.age, "years old.")
}
type student struct {
*person
school string
}
func main() {
p := person{
name: "John",
age: 20,
}
s := student{
person: &p,
school: "ABC School",
}
s.talk()
}
Copy after login

In the above example, a `person` structure is defined and a `talk` is defined for it method. Then a `student` structure is defined and the `person` structure is nested as its field. In the `main` function, a variable `p` of type `person` is first created, then a variable `s` of type `student` is created, and `p` is assigned to `person` in `s`. Field to implement access to the `talk` method. Finally, the student's name and age are output.

Conclusion

This article introduces several ways to implement abstract methods in Go language, including interfaces, function types and type combinations. Through these methods, the reusability and scalability of the code can be improved, making the code more flexible and easier to maintain. Developers can choose a suitable implementation method based on actual needs to achieve the best programming effect.

The above is the detailed content of What are the abstract methods in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template