英文:
how to use generics to merge two function in one in golang
问题
我已经翻译了你的代码,如下所示:
func concat(args ...[]directory_account_price_discount_tax) []directory_account_price_discount_tax {
concated := []directory_account_price_discount_tax{}
for _, i := range args {
concated = append(concated, i...)
}
return concated
}
func concat_strings_slice(args ...[]string) []string {
concated := []string{}
for _, i := range args {
concated = append(concated, i...)
}
return concated
}
type directory_account_price_discount_tax struct {
directory_no []int
Account string
Price, Discount, Tax float64
}
请注意,Go语言目前还不支持泛型,因此无法直接在函数中使用泛型。你需要根据具体的类型进行函数重载或使用接口来实现相似的功能。
英文:
i have this two functions and i want to implement generics on it. just to know i tried interface{}
and [t any]
and (t any)
and all field. and this is the code. if you know how to use generics to make this two functions in one function. just write the code
func concat(args ...[]directory_account_price_discount_tax) []directory_account_price_discount_tax {
concated := []directory_account_price_discount_tax{}
for _, i := range args {
concated = append(concated, i...)
}
return concated
}
func concat_strings_slice(args ...[]string) []string {
concated := []string{}
for _, i := range args {
concated = append(concated, i...)
}
return concated
}
and the struct of directory_account_price_discount_tax
is
type directory_account_price_discount_tax struct {
directory_no []int
Account string
Price, Discount, Tax float64
}
答案1
得分: 1
使用reflect包编写一个函数,该函数可以连接任意类型的切片。
func concat(args ...interface{}) interface{} {
n := 0
for _, arg := range args {
n += reflect.ValueOf(arg).Len()
}
v := reflect.MakeSlice(reflect.TypeOf(args[0]), 0, n)
for _, arg := range args {
v = reflect.AppendSlice(v, reflect.ValueOf(arg))
}
return v.Interface()
}
当没有参数或参数类型不同时,该函数会引发 panic。
可以像这样使用concat
函数:
s := concat([]string{"a", "b"}, []string{"c", "d"}).([]string)
注意使用type assertion将结果转换为所需的类型。
虽然有一种更好的解决方案,可以使用Go的type parameters特性,但该特性在我撰写此答案时尚未发布。
英文:
Use the reflect package to write a function that concatenates slices of an arbitrary type.
func concat(args ...interface{}) interface{} {
n := 0
for _, arg := range args {
n += reflect.ValueOf(arg).Len()
}
v := reflect.MakeSlice(reflect.TypeOf(args[0]), 0, n)
for _, arg := range args {
v = reflect.AppendSlice(v, reflect.ValueOf(arg))
}
return v.Interface()
}
The function panics when there are no arguments or the arguments are not the same type.
Use the concat
function like this:
s := concat([]string{"a", "b"}, []string{"c", "d"}).([]string)
Note the use of the type assertion to get result as the desired type.
There is a better solution using Go's type parameters feature, but that feature is not released as of the time I am writing this answer.
答案2
得分: 0
这是如何接受任何类型的值并将它们附加到数组的方法。
func concat(args ...interface{}) []interface{} {
concated := make([]interface{}, 0)
for _, i := range args {
concated = append(concated, i)
}
return concated
}
但是当你需要访问directory_account_price_discount_tax
的字段时,你需要进行类型断言,像这样:
arr := concat(/* 一些 directory_account_price_discount_tax 类型的值 */)
v := arr[0].(directory_account_price_discount_tax)
英文:
This is how you can accept any kind of value and append them to an array.
func concat(args ...interface{}) []interface{} {
concated := make([]interface{}, 0)
for _, i := range args {
concated = append(concated, i)
}
return concated
}
But when you need to access the fields of directory_account_price_discount_tax
you need type assertion like this
arr := concat(/* some directory_account_price_discount_tax type values */)
v := arr[0].(directory_account_price_discount_tax)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论