Quick actions

cmd+k|ctrl+k

Navigation

Languages

Abstraction OOP

Snippet info

Language

Go

Visibility

public

Author

riki.whyudi

Created

2023-11-08T01:26:45.42964Z

Updated

2023-11-08T01:26:45.42964Z

package main

import "fmt"

type Animal interface {
	Speak()
}

type Dog struct {
	Name string
}

func NewDog(name string) *Dog {
	return &Dog{Name: name}
}

func (d Dog) Speak() {
	fmt.Printf("%s says Woof!\n", d.Name)
}

type Cat struct {
	Name string
}

func NewCat(name string) *Cat {
	return &Cat{Name: name}
}

func (c Cat) Speak() {
	fmt.Printf("%s says Meow!\n", c.Name)
}

func main() {
	dog := NewDog("Buddy")
	cat := NewCat("Whiskers")

	animals := []Animal{dog, cat}

	for _, animal := range animals {
		animal.Speak()
	}
}
INFO