Skip to content

Instantly share code, notes, and snippets.

@madflojo
Last active August 23, 2022 02:27
Show Gist options
  • Save madflojo/c369c1620ae305a75b2d1654815c67d9 to your computer and use it in GitHub Desktop.
Save madflojo/c369c1620ae305a75b2d1654815c67d9 to your computer and use it in GitHub Desktop.
Threadsafe Packages Article: Base Structure
/*
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
// 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) {
return &Directory{}, nil
}
// Name will return the current Directory name.
func (d *Directory) Name() string {
return ""
}
// SetName will change the name of the current Directory.
func (d *Directory) SetName(n string) error {
return nil
}
// Add will add a person to the internal directory.
func (d *Directory) Add(p Person) error {
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) {
return Person{}, nil
}
// Directory will return a map of all people stored within the Directory.
func (d *Directory) Directory() map[string]Person {
m := make(map[string]Person)
return m
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment