animals

package
v0.1.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package animals provides a simple abstraction for representing animals.

I'm so sorry this is basic and contrived. I'm not a zoologist.

Index

Constants

View Source
const IS_ANIMAL = true

This is not used at all. I just put this here so the docs don't feel lonely

Variables

View Source
var Breed_value = map[string]DogBreed{
	"corgi":    Corgi,
	"frenchie": Frenchie,
	"poodle":   Poodle,
	"mutt":     Mutt,
}

Functions

func ShowAnimal

func ShowAnimal(animal Animal)

Types

type Animal

type Animal interface {
	Display()
}

Animal is an interface that defines the behavior of an animal. It's used to demonstrate how interfaces work in Go during the workshop. I completely understand that this is a contrived example and that animals in real life don't have a Display method.

type Dog

type Dog struct {
	Name  string   `json:"name"`
	Age   int      `json:"age"`
	Breed DogBreed `json:"breed"`
}

Dog is a representation of a dog and contains a name, age, and breed. Don't use this type directly, use NewDog instead.

func NewDog

func NewDog(name string, age int, breed DogBreed) (*Dog, error)

NewDog creates a new instance of Dog with the specified values.

  • name - a unique name for this dog; common names include 'fido' and 'max'.
  • age - the current age of the dog. Must be greater than zero.
  • breed - the breed of the dog. Use the DogBreed constants to specify the breed.

func (*Dog) Display

func (d *Dog) Display()

Display prints the dog's information in a nice format. It's printed to stdout.

func (*Dog) SetAge

func (d *Dog) SetAge(age int)

SetAge is a setter method that updates the dog's age. This is used in the workshop portion that discusses pointers.

  • age - the new age of the dog. Must be greater than zero.

type DogBreed

type DogBreed int
const (
	Corgi    DogBreed = 0
	Frenchie DogBreed = 1
	Poodle   DogBreed = 2
	Mutt     DogBreed = 3
)

func FromString

func FromString(s string) (DogBreed, error)

func (DogBreed) IsMutt

func (breed DogBreed) IsMutt() bool

func (DogBreed) MarshalJSON added in v0.1.14

func (breed DogBreed) MarshalJSON() ([]byte, error)

func (DogBreed) String

func (breed DogBreed) String() string

func (*DogBreed) UnmarshalJSON added in v0.1.14

func (s *DogBreed) UnmarshalJSON(data []byte) (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL