Created
June 4, 2019 02:17
-
-
Save eminetto/b0156e40470469d2f275e68da6f1c603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package contact | |
import ( | |
"strings" | |
"fmt" | |
) | |
type friends struct { | |
data []string | |
} | |
type person struct { | |
name string | |
friends *friends | |
} | |
func NewFriends() *friends { | |
return &friends{ | |
data: []string{}, | |
} | |
} | |
func (f *friends) Add(name string) { | |
f.data = append(f.data, name) | |
} | |
func (f *friends) Remove(name string) { | |
new := []string{} | |
for _, friend := range f.data { | |
if friend != name { | |
new = append(new, friend) | |
} | |
} | |
f.data = new | |
} | |
func (f *friends) String() string { | |
return strings.Join(f.data, " ") | |
} | |
func NewPerson(name string) *person { | |
return &person{ | |
name: name, | |
friends: NewFriends(), | |
} | |
} | |
func (p *person) AddFriend(name string) { | |
p.friends.Add(name) | |
} | |
func (p *person) RemoveFriend(name string) { | |
p.friends.Remove(name) | |
} | |
func (p *person) GetFriends() *friends { | |
return p.friends | |
} | |
func (p *person) String() string { | |
return fmt.Sprintf("%s [%v]", p.name, p.friends) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment