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

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

Adding to an anonymous slice in a type in golang

问题

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

package main

import "fmt"

type MyType struct{
    Name string
    Something string
}

type MyTypes []*MyType 

func NewMyTypes(myTypes ...*MyType)*MyTypes{
    var s MyTypes = myTypes
    return &s
}

//example of a method I want to be able to add to a slice
func(m MyTypes) Key() string{
    var result string
    
    for _,i := range m{
        result += i.Name + ":" 
    }
    
    return result
}


func main() {
    mytype1 ,mytype2 := MyType{Name:"Joe", Something: "Foo"},  MyType{Name:"PeggySue", Something: "Bar"}
    
    myTypes:= NewMyTypes(&mytype1,&mytype2) 
    
    //cant use it as a slice sadface
    //myTypes = append(myTypes,&MyType{Name:"Random", Something: "asdhf"})
    
    fmt.Println(myTypes.Key())
}

我不想将其包装在另一个类型中,并命名参数,尽管我有点这样做。因为 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.

package main

import "fmt"


type MyType struct{
	Name string
	Something string
}


type MyTypes []*MyType 

func NewMyTypes(myTypes ...*MyType)*MyTypes{
	var s MyTypes = myTypes
	return &s
}

//example of a method I want to be able to add to a slice
func(m MyTypes) Key() string{
	var result string
	
	for _,i := range m{
		result += i.Name + ":" 
	}
	
	return result
}


func main() {
	mytype1 ,mytype2 := MyType{Name:"Joe", Something: "Foo"},  MyType{Name:"PeggySue", Something: "Bar"}
	
	myTypes:= NewMyTypes(&mytype1,&mytype2) 
	
	//cant use it as a slice sadface
	//myTypes = append(myTypes,&MyType{Name:"Random", Something: "asdhf"})
	
	fmt.Println(myTypes.Key())
}

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更优雅的方法。这是他更优雅的方法:

package main

import (
	"fmt"
	"strings"
)

type MyType struct {
	Name      string
	Something string
}

type MyTypes []*MyType

func NewMyTypes(myTypes ...*MyType) MyTypes {
	return myTypes
}

//example of a method I want to be able to add to a slice
func (m MyTypes) Names() []string {
	names := make([]string, 0, len(m))
	for _, v := range m {
		names = append(names, v.Name)
	}
	return names
}

func main() {
	mytype1, mytype2 := MyType{Name: "Joe", Something: "Foo"}, MyType{Name: "PeggySue", Something: "Bar"}
	myTypes := NewMyTypes(&mytype1, &mytype2)
	myTypes = append(myTypes, &MyType{Name: "Random", Something: "asdhf"})
	fmt.Println(strings.Join(myTypes.Names(), ":"))
}

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:

package main

import (
	"fmt"
	"strings"
)

type MyType struct {
	Name      string
	Something string
}

type MyTypes []*MyType

func NewMyTypes(myTypes ...*MyType) MyTypes {
	return myTypes
}

//example of a method I want to be able to add to a slice
func (m MyTypes) Names() []string {
	names := make([]string, 0, len(m))
	for _, v := range m {
		names = append(names, v.Name)
	}
	return names
}

func main() {
	mytype1, mytype2 := MyType{Name: "Joe", Something: "Foo"}, MyType{Name: "PeggySue", Something: "Bar"}
	myTypes := NewMyTypes(&mytype1, &mytype2)
	myTypes = append(myTypes, &MyType{Name: "Random", Something: "asdhf"})
	fmt.Println(strings.Join(myTypes.Names(), ":"))
}

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:

确定