I'm trying to play around with recursive structs, where when I have multiple I can add them together, creating a new struct with those embedded. However, I'm not sure what the proper way to approach this is.
I've included a code snippet below to further illustrate what I mean.
package main
import "fmt"
type Container struct {
F int
Collection []SubContainer
}
type SubContainer struct {
Key string
Value int
}
func main() {
commits := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
sc := []SubContainer{}
c := []Container{}
count := 0
for k, v := range commits {
sc = append(sc, SubContainer{Key: k, Value: v})
count++
if len(sc) == 2 {
c = append(c, Container{Collection: sc, F: count})
sc = nil
}
}
for _, r := range c {
fmt.Println(r)
}
}
Result:
{2 [{a 1} {b 2}]}
{4 [{c 3} {d 4}]}
Desired result:
{6 {2 [{a 1} {b 2}]} {4 [{c 3} {d 4}]}}
Playground link: https://play.golang.org/p/j6rbhgcOoT
One caveat I'm having trouble wrapping my head around is that the commits
length may change (I was initially thinking I could just create a different parent struct). Any suggestions would be appreciated... Is doing this somehow with recursive structs the right approach to accomplish this? Thanks!
I attempted to get close to the desired output without being sure about the exact goal, you will find below a modified version of the snippet you provided.
You can use the String() string
method on a collection to customize the format.
package main
import "fmt"
type ContainerCollection struct {
Count int
List []Container
}
func (cc ContainerCollection) String() string {
total := 0
for _, c := range cc.List {
total += c.F
}
return fmt.Sprintf("{%d %v}", total, cc.List)
}
type Container struct {
F int
Collection []SubContainer
}
type SubContainer struct {
Key string
Value int
}
func main() {
commits := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
c := ContainerCollection{Count: 0}
sc := []SubContainer{}
for k, v := range commits {
sc = append(sc, SubContainer{Key: k, Value: v})
c.Count++
if len(sc) == 2 {
c.List = append(c.List, Container{Collection: sc, F: c.Count})
sc = []SubContainer{}
}
}
// Should also cover odd number of commits
if len(sc) != 0 {
c.Count++
c.List = append(c.List, Container{Collection: sc, F: c.Count})
}
// for _, r := range c.List { fmt.Println(r) }
fmt.Println(c)
}
Result:
{6 [{2 [{a 1} {b 2}]} {4 [{c 3} {d 4}]}]}
Here's something with minimal modification to your code (just added a 'super' container, which is basically a summary struct). One probably need this only if this is being passed to another library/package/over the net etc., otherwise just maintaining the totalCount
may be enough.
package main
import "fmt"
type SuperContainer struct {
TotalCount int
Containers []Container
}
type Container struct {
F int
Collection []SubContainer
}
type SubContainer struct {
Key string
Value int
}
func main() {
var totalCount int
commits := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
sc := []SubContainer{}
c := []Container{}
count := 0
for k, v := range commits {
sc = append(sc, SubContainer{Key: k, Value: v})
count++
if len(sc) == 2 {
totalCount += count
c = append(c, Container{Collection: sc, F: count})
sc = nil
}
}
for _, r := range c {
fmt.Println(r)
}
supC := SuperContainer{TotalCount: totalCount, Containers: c}
fmt.Println(supC)
}
Playground: https://play.golang.org/p/yN3N3gHaCX