Created
June 4, 2019 02:17
-
-
Save eminetto/7e5d4d3e33cc09adf0f1e5b547f9896b 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 "fmt" | |
type person struct { | |
name string | |
friends []string | |
} | |
type friend struct { | |
name string | |
} | |
func NewPerson(name string) *person { | |
return &person{ | |
name: name, | |
friends: []string{}, | |
} | |
} | |
func (p *person) AddFriend(name string) { | |
p.friends = append(p.friends, name) | |
} | |
func (p *person) RemoveFriend(name string) { | |
new := []string{} | |
for _, friend := range p.friends { | |
if friend != name { | |
new = append(new, friend) | |
} | |
} | |
p.friends = new | |
} | |
func (p *person) GetFriends() []string { | |
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