Is this interface well implemented?

package main

import "fmt"

type Greetings interface{
  SayHello() string
}

type Person struct{
  Hello string
}

func (p Person) SayHello() string{
  return p.Hello
}

func PersonGreeting(g Greetings){
  fmt.Println(g.SayHello())
}

func main() {
  illud := Person{Hello: "Yo!"}
  harpyja := Person{Hello: "Whats up!"}
  PersonGreeting(illud)
  PersonGreeting(harpyja)
}

Can you rephrase your question? Are you asking if Person satisfies the Greetings interface, or if the Greetings interface definition itself is a good interface, or something else?

Im new in interfaces so im just asking if all my code is well implemented. so,
im asking if Person satisfies the Greetings interface and if the Greetings interface definition itself is a good interface.

The code compiles and runs properly in the Playground, so I believe that confirms that the interface is satisfied. When you want to confirm that a concrete type implements an interface, you can write this:

var _ Greetings = Person{}

which will give you an error at compile time if the interface is not satisfied.

As for if it’s a “good” interface or not; that is impossible to answer without knowing how you intend to use it. I’m under the impression that this is a contrived example and you plan on adapting it to another problem. If that is the case, I think we could give you a better answer if we saw that problem. The same interface doesn’t always apply to every problem; otherwise, we’d only ever need io.Reader!

If this is not a contrived example and you are perhaps building some interactive dialog (chatbot, video game NPC, etc.), then I still think we need to know your requirements and the infrastructure around this code to adequately answer your question. If you only need to support “interjection-like” greetings in English, then the interface will work. If you need more than that, then you need more context, probably with one or more parameters into SayHello. For example:

  • I might greet friends and family with “hi,” but I’d greet my boss with “hello,” or “good morning,” so to accommodate that requirement, SayHello might need to look like this SayHello(to Whom) string.

  • If you need to support other languages, then you might need to know more information about who you’re saying hello to. For example, in Irish, you need to put the “recipient” of the greeting into the vocative case, so greeting someone would need to change that someone to some other form. (e.g. my name is Sean, but if you wanted to say hello to me in Irish, you need to say “Dia dhuit, a Sheain”).

  • Maybe you need the Greetings implementation to know what time of day it is so they say “Good morning” in the morning and “good afternoon” in the afternoon.

If these topics are a concern, some or all of them might be addressed by the infrastructure around the Greetings interface. Maybe you’ll have a GreetingsFor(ctx context.Context, to Whom) Greetings function that picks a Greetings implementation based on all (or some) of these parameters so that Greetings itself only needs to have a SayHello() string function.

Or maybe just saying “Hey!” or “Yo!” is enough for your problem! It all depends on the solution’s constraints/requirements!

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.