在for循环中一次添加两个项目到结构体中。

huangapple go评论84阅读模式
英文:

Add two items at a time to struct in for loop

问题

我正在尝试一次向结构数组中添加两个项目,然后每两个项目连续创建一个新的结构数组,并将其附加到最终的Container结构中。我在寻找正确的方法方面遇到了困难。

为了进一步说明我的意思:

package main

import "fmt"

type Container struct {
    Collection []SubContainer
}

type SubContainer struct {
    Key   string
    Value int
}

func main() {
    commits := map[string]int{
        "a": 1,
        "b": 2,
        "c": 3,
        "d": 4,
        "e": 5,
        "f": 6,
    }

    sc := []SubContainer{}
    c := Container{}

    for k, v := range commits {
        sc = append(sc, SubContainer{Key: k, Value: v})
    }

    for _, s := range sc {
        c.Collection = append(c.Collection, s)
    }

    fmt.Println(c)
}

我希望循环遍历所有的commits,每当SubContainer达到len(2)时,将其附加到Container并创建一个新的SubContainer,直到for循环完成。如果元素数量不均匀,则最后一个SubContainer只包含一个元素,并像正常情况下一样附加到Container中。

有人对如何实现这个有什么建议吗?如果这是一个明显的答案,对不起,我对Go还很陌生!

英文:

I'm trying to add 2 items at once to a struct array, then continuously every 2 items create a new struct array and append to the final Container struct. I'm struggling to find the proper way of doing this.

To further illustrate what I mean:

package main

import "fmt"

type Container struct {
	Collection []SubContainer
}

type SubContainer struct {
	Key   string
	Value int
}

func main() {
	commits := map[string]int{
		"a": 1,
		"b": 2,
		"c": 3,
		"d": 4,
		"e": 5,
		"f": 6,
	}

	sc := []SubContainer{}
	c := Container{}

	for k, v := range commits {
		sc = append(sc, SubContainer{Key: k, Value: v})
	}

	for _, s := range sc {
		c.Collection = append(c.Collection, s)
	}

	fmt.Println(c)
}

Link: https://play.golang.org/p/OhSntFT7Hp

My desired behaviour is to loop through all the commits, and every time the SubContainer reaches len(2), append to the Container, and create a new SubContainer until the for loop is complete. If there's an uneven number of elements, then the last SubContainer would just hold one element and append to Container like normal.

Does anyone have any suggestions on how to do this? Apologies if this is a obvious answer, very new to Go!

答案1

得分: 1

你可以通过将切片设置为nil来“重置”它。此外,你可能不知道nil切片在使用append函数时也能正常工作:

var sc []SubContainer
c := Container{}

for k, v := range commits {
    sc = append(sc, SubContainer{Key: k, Value: v})
    if len(sc) == 2 {
        c.Collection = append(c.Collection, sc...)
        sc = nil
    }
}
if len(sc) > 0 {
    c.Collection = append(c.Collection, sc...)
}

你可以在这里查看示例代码:https://play.golang.org/p/ecj52fkwpO

英文:

You can "reset" a slice by setting it to nil. Also, you may not be aware that nil-slices work just fine with append:

var sc []SubContainer
c := Container{}

for k, v := range commits {
	sc = append(sc, SubContainer{Key: k, Value: v})
	if len(sc) == 2 {
		c.Collection = append(c.Collection, sc...)
		sc = nil
	}
}
if len(sc) > 0 {
	c.Collection = append(c.Collection, sc...)

}

https://play.golang.org/p/ecj52fkwpO

huangapple
  • 本文由 发表于 2017年8月25日 19:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/45880883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定