Back to Developer Roadmap

Code Reusability

src/data/question-groups/golang/content/code-reusability.md

4.0639 B
Original Source

The signature of a method defines how a function or method should be called and what parameters and return values are expected. This clear contract makes your code flexible and reusable. For example:

go
package main

import "fmt"

type Greeter interface {
    Greet() string
}

// Person struct implements the Greeter interface
type Person struct {
    Name string
}

func (p Person) Greet() string {
    return "Hello, " + p.Name + "!"
}

// SayHello accepts any type that satisfies the interface value Greeter.
func SayHello(g Greeter) {
    fmt.Println(g.Greet())
}

func main() {
    p := Person{Name: "John"}
    SayHello(p)
}