func (block *blockEvent) toFilteredBlock() (*peer.FilteredBlock, error)
In the function definition above, toFilteredBlock is the function name (no factor) and
If the return value appears to be (*peer.FilteredBlock, error), what does the first (block *blockEvent) mean?
go
It's called a receiver.
Think of it as an easy way to declare a method.
The GO language is not an object-oriented language.
So there's no class, there's no inheritance.
If a structure exists and a function is declared as a receiver, it can be used like an object-oriented method. Inheritance is also implemented as a composition through embedding. That is, create a hasa relationship.
Polymorphism is possible through interface.
Please refer to the example below.
package main
import (
"fmt"
)
type Person interface {
Walk()
Sleep()
}
type Man struct {
age int
addr string
name string
}
func (man *Man) Walk() {
fmt.Println ("The Man Walks".")
}
func (man *Man) Sleep() {
fmt.Println ("The man sleeps.")
}
func CreateMan(age int, addr string, name string) (man *Man) {
man = &Man{age: age, addr: addr, name: name}
return man
}
func Execute(person Person) {
person.Walk()
person.Sleep()
}
func main() {
var person Person
person = CreateMan(10, "aaaa", "name")
fmt.Println(person)
Execute(person)
}
© 2024 OneMinuteCode. All rights reserved.