Last active
August 26, 2015 23:55
-
-
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
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 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