I have a basic question for Golang.

Asked 2 years ago, Updated 2 years ago, 44 views

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

2022-09-22 18:32

1 Answers

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)
}


2022-09-22 18:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.