在golang中向匿名切片添加元素。

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

Adding to an anonymous slice in a type in golang

问题

我想在一个切片上添加一些辅助方法。所以我创建了一个类型 []*MyType。有没有办法向这个 MyTypes 切片添加元素?append 方法无法识别这个切片。

  1. package main
  2. import "fmt"
  3. type MyType struct{
  4. Name string
  5. Something string
  6. }
  7. type MyTypes []*MyType
  8. func NewMyTypes(myTypes ...*MyType)*MyTypes{
  9. var s MyTypes = myTypes
  10. return &s
  11. }
  12. //example of a method I want to be able to add to a slice
  13. func(m MyTypes) Key() string{
  14. var result string
  15. for _,i := range m{
  16. result += i.Name + ":"
  17. }
  18. return result
  19. }
  20. func main() {
  21. mytype1 ,mytype2 := MyType{Name:"Joe", Something: "Foo"}, MyType{Name:"PeggySue", Something: "Bar"}
  22. myTypes:= NewMyTypes(&mytype1,&mytype2)
  23. //cant use it as a slice sadface
  24. //myTypes = append(myTypes,&MyType{Name:"Random", Something: "asdhf"})
  25. fmt.Println(myTypes.Key())
  26. }

我不想将其包装在另一个类型中,并命名参数,尽管我有点这样做。因为 JSON 编组可能会有所不同。

有什么方法可以向 MyTypes 切片添加元素?

我真的希望能够只是向切片添加一个方法,以便它可以实现特定的接口,而不影响编组。有没有其他更好的方法?

谢谢

英文:

I want to add a few helper methods attached onto a slice.
So I created a type which is of []*MyType
Is there any way to add to that slice of MyTypes? append will not recognise the slice.

  1. package main
  2. import "fmt"
  3. type MyType struct{
  4. Name string
  5. Something string
  6. }
  7. type MyTypes []*MyType
  8. func NewMyTypes(myTypes ...*MyType)*MyTypes{
  9. var s MyTypes = myTypes
  10. return &s
  11. }
  12. //example of a method I want to be able to add to a slice
  13. func(m MyTypes) Key() string{
  14. var result string
  15. for _,i := range m{
  16. result += i.Name + ":"
  17. }
  18. return result
  19. }
  20. func main() {
  21. mytype1 ,mytype2 := MyType{Name:"Joe", Something: "Foo"}, MyType{Name:"PeggySue", Something: "Bar"}
  22. myTypes:= NewMyTypes(&mytype1,&mytype2)
  23. //cant use it as a slice sadface
  24. //myTypes = append(myTypes,&MyType{Name:"Random", Something: "asdhf"})
  25. fmt.Println(myTypes.Key())
  26. }

I don't want to wrap it in another type and name the param even though I'm sorta doing it.. Because of json marshalling will probably be different

What would be the way to add to the MyTypes slice?

I really want to just be able to add a method to a slice so it can implement a specific interface and not effect the marshalling.. Is there a different better way?

Thanks

答案1

得分: 2

更新:这个答案曾经包含了两种解决问题的方法:我有点笨拙的方法和DaveC更优雅的方法。这是他更优雅的方法:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type MyType struct {
  7. Name string
  8. Something string
  9. }
  10. type MyTypes []*MyType
  11. func NewMyTypes(myTypes ...*MyType) MyTypes {
  12. return myTypes
  13. }
  14. //example of a method I want to be able to add to a slice
  15. func (m MyTypes) Names() []string {
  16. names := make([]string, 0, len(m))
  17. for _, v := range m {
  18. names = append(names, v.Name)
  19. }
  20. return names
  21. }
  22. func main() {
  23. mytype1, mytype2 := MyType{Name: "Joe", Something: "Foo"}, MyType{Name: "PeggySue", Something: "Bar"}
  24. myTypes := NewMyTypes(&mytype1, &mytype2)
  25. myTypes = append(myTypes, &MyType{Name: "Random", Something: "asdhf"})
  26. fmt.Println(strings.Join(myTypes.Names(), ":"))
  27. }

Playground: https://play.golang.org/p/FxsUo1vu6L

英文:

Update: This answer once contained two ways to solve the problem: my somewhat clunky way, the DaveC's more elegant way. Here's his more elegant way:

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type MyType struct {
  7. Name string
  8. Something string
  9. }
  10. type MyTypes []*MyType
  11. func NewMyTypes(myTypes ...*MyType) MyTypes {
  12. return myTypes
  13. }
  14. //example of a method I want to be able to add to a slice
  15. func (m MyTypes) Names() []string {
  16. names := make([]string, 0, len(m))
  17. for _, v := range m {
  18. names = append(names, v.Name)
  19. }
  20. return names
  21. }
  22. func main() {
  23. mytype1, mytype2 := MyType{Name: "Joe", Something: "Foo"}, MyType{Name: "PeggySue", Something: "Bar"}
  24. myTypes := NewMyTypes(&mytype1, &mytype2)
  25. myTypes = append(myTypes, &MyType{Name: "Random", Something: "asdhf"})
  26. fmt.Println(strings.Join(myTypes.Names(), ":"))
  27. }

Playground: https://play.golang.org/p/FxsUo1vu6L

huangapple
  • 本文由 发表于 2015年10月2日 12:49:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/32900726.html
匿名

发表评论

匿名网友

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

确定