Skip to content

Instantly share code, notes, and snippets.

@madflojo
Created August 24, 2022 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madflojo/dbc14c55448de718f682519599911d74 to your computer and use it in GitHub Desktop.
Save madflojo/dbc14c55448de718f682519599911d74 to your computer and use it in GitHub Desktop.
Threadsafe Packages - Base with Logic but unsafe
/*
Package directory is an example package that creates an internal directory for People.
This package is an example used for a Blog article and is not for anything else.
*/
package directory
import (
"fmt"
)
// Person represents a person and their attributes.
type Person struct {
// Age represents the persons Age
Age int
// Name represents the persons Name
Name string
}
// Directory provides the ability to store and lookup people.
type Directory struct {
// directory stores known People
directory map[string]Person
// name is used to identify the Directory instance
name string
}
// New is used to create a new instance of Directory.
func New(name string) (*Directory, error) {
d := &Directory{}
// Validate Name
if name == "" {
return d, fmt.Errorf("cannot have an empty name")
}
d.name = name
// Create new map
d.directory = make(map[string]Person)
return d, nil
}
// Name will return the current Directory name.
func (d *Directory) Name() string {
return d.name
}
// SetName will change the name of the current Directory.
func (d *Directory) SetName(n string) error {
if n == "" {
return fmt.Errorf("cannot have an empty name")
}
d.name = n
return nil
}
// Add will add a person to the internal directory.
func (d *Directory) Add(p Person) error {
if p.Name == "" {
return fmt.Errorf("name cannot be empty")
}
// Guess we can't have two people with the same name...
d.directory[p.Name] = p
return nil
}
// Lookup will find and return a person. If the provided person does not exist, Lookup will return
// an error.
func (d *Directory) Lookup(name string) (Person, error) {
p, ok := d.directory[name]
if ok {
return p, nil
}
return Person{}, fmt.Errorf("could not find person")
}
// Directory will return a map of all people stored within the Directory.
func (d *Directory) Directory() map[string]Person {
return d.directory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment