Skip to content

Instantly share code, notes, and snippets.

@scott-maddox
Last active August 26, 2015 23:55
Show Gist options
  • Save scott-maddox/cbc20625414f6ec93098 to your computer and use it in GitHub Desktop.
Save scott-maddox/cbc20625414f6ec93098 to your computer and use it in GitHub Desktop.
An alternative solution to the problem posed in this blog post: http://www.onebigfluke.com/2014/12/generic-programming-go-generate.html
package main
import (
"fmt"
"strings"
)
type Person struct {
FirstName string
LastName string
HairColor string
}
func (s *Person) String() string {
return fmt.Sprintf("%#v", s)
}
type People []Person
func (j People) String() string {
stred := make([]string, 0, len(j))
for _, s := range j {
stred = append(stred, s.String())
}
return strings.Join(stred, "\n")
}
func main() {
people := People{
Person{"Sideshow", "Bob", "red"},
Person{"Homer", "Simpson", "n/a"},
Person{"Lisa", "Simpson", "blonde"},
Person{"Marge", "Simpson", "blue"},
Person{"Mr", "Burns", "gray"},
}
fmt.Printf("My favorite Simpsons Characters:\n%s\n", people)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment