将不同类型的参数传递给函数

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

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 &lt; len(arr); {
		for j := 0; j &lt; 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 &quot;fmt&quot;

func main() {
	x := []interface{}{&quot;bob&quot;, &quot;doug&quot;, &quot;bob&quot;}
	fmt.Println(RemoveDuplicate(x))
	y := []interface{}{1, 3, 1}
	fmt.Println(RemoveDuplicate(y))
	z := []interface{}{&quot;bob&quot;, &quot;2&quot;, &quot;doug&quot;, 3, 2, &quot;bob&quot;}
	fmt.Println(RemoveDuplicate(z))	
}

func RemoveDuplicate(arr []interface{}) []interface{} {
    arr2 := arr[:1]
Loop:
    for i := 1; i &lt; len(arr); {
        for j := 0; j &lt; 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 &lt; s.Len(); i++ {
		for j := 0; j &lt; 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.

huangapple
  • 本文由 发表于 2015年7月11日 17:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/31355426.html
匿名

发表评论

匿名网友

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

确定