英文:
Passing different type's parameters to the function
问题
我有这个函数,我想让它能够接收所有类型的切片,不仅仅是 []string,还有 []int 等等...我想知道在将参数传递给函数头时是否有一种抽象类型的方法,或者我应该做其他事情来实现这个。
package removeDuplicate
// RemoveDuplicate 从切片中删除重复项,并将结果存储在 arr2 中
func RemoveDuplicate(arr []string) []string {
arr2 := arr[:1]
Loop:
for i := 1; i < len(arr); {
for j := 0; j < len(arr2); {
if arr[i] != arr[j] {
j++
} else {
i++
continue Loop
}
}
arr2 = append(arr2, arr[i])
i++
}
return arr2
}
提前感谢 =]
英文:
I have this function and i would like to make it be able to receive all types of slices, not only []string, but []int and so on... I would like to know if is there some way to abstract the type when passing the parameter to the function header or if i should do other thing to accomplish that.
package removeDuplicate
// RemoveDuplicate remove duplicate items from slice setting it to arr2
func RemoveDuplicate(arr []string) []string {
arr2 := arr[:1]
Loop:
for i := 1; i < len(arr); {
for j := 0; j < len(arr2); {
if arr[i] != arr[j] {
j++
} else {
i++
continue Loop
}
}
arr2 = append(arr2, arr[i])
i++
}
return arr2
}
Thanks in advance =]
答案1
得分: 2
如果你将函数签名更改为接受interface{},你可以得到适用于内置类型的函数。
package main
import "fmt"
func main() {
x := []interface{}{"bob", "doug", "bob"}
fmt.Println(RemoveDuplicate(x))
y := []interface{}{1, 3, 1}
fmt.Println(RemoveDuplicate(y))
z := []interface{}{"bob", "2", "doug", 3, 2, "bob"}
fmt.Println(RemoveDuplicate(z))
}
func RemoveDuplicate(arr []interface{}) []interface{} {
arr2 := arr[:1]
Loop:
for i := 1; i < len(arr); {
for j := 0; j < len(arr2); {
if arr[i] != arr[j] {
j++
} else {
i++
continue Loop
}
}
arr2 = append(arr2, arr[i])
i++
}
return arr2
}
请查看FAQ Can I convert a []T to an []interface{}?(以及前面的问题)以获取更多信息。
英文:
If you alter the function signature to accept interface{} you can get something that works on built in types.
package main
import "fmt"
func main() {
x := []interface{}{"bob", "doug", "bob"}
fmt.Println(RemoveDuplicate(x))
y := []interface{}{1, 3, 1}
fmt.Println(RemoveDuplicate(y))
z := []interface{}{"bob", "2", "doug", 3, 2, "bob"}
fmt.Println(RemoveDuplicate(z))
}
func RemoveDuplicate(arr []interface{}) []interface{} {
arr2 := arr[:1]
Loop:
for i := 1; i < len(arr); {
for j := 0; j < len(arr2); {
if arr[i] != arr[j] {
j++
} else {
i++
continue Loop
}
}
arr2 = append(arr2, arr[i])
i++
}
return arr2
}
Have a look at the FAQ Can I convert a []T to an []interface{}? (and the one before) for more information.
答案2
得分: 0
在Go语言中,可以使用接口和反射两种机制来实现任何类型的通用算法。使用接口的方式与sort
包类似:
type Slice interface {
Len() int
Swap(i, j int)
Eq(i, j int) bool
SubSlice(i, j int) Slice
}
func RemoveDuplicate(s Slice) Slice {
n := 1
Loop:
for i := 1; i < s.Len(); i++ {
for j := 0; j < n; j++ {
if s.Eq(i, j) {
continue Loop
}
}
s.Swap(n, i)
n++
}
return s.SubSlice(0, n)
}
使用整数和字符串的示例代码可以在这里查看:Playground。
英文:
Any kind of generic algorithms in Go can be implemented with either of two mechanisms: interfaces and reflection. With interfaces, you can do it similarly to the sort
package:
type Slice interface {
Len() int
Swap(i, j int)
Eq(i, j int) bool
SubSlice(i, j int) Slice
}
func RemoveDuplicate(s Slice) Slice {
n := 1
Loop:
for i := 1; i < s.Len(); i++ {
for j := 0; j < n; j++ {
if s.Eq(i, j) {
continue Loop
}
}
s.Swap(n, i)
n++
}
return s.SubSlice(0, n)
}
Playground with ints and strings: http://play.golang.org/p/WwC27eP72n.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论