Skip to content

Instantly share code, notes, and snippets.

@timbogit
Last active December 27, 2015 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timbogit/7268248 to your computer and use it in GitHub Desktop.
Save timbogit/7268248 to your computer and use it in GitHub Desktop.
Variadic function of interfaces called with a string slice
package main
import (
"fmt"
)
type TestStruct struct {
int
string
}
func UniqueOf(slice ...interface{}) []interface{} {
seen := make(map[interface{}]int, len(slice))
result := make([]interface{}, len(slice))
resultIndex := 0
for _, val := range slice {
if _, ok := seen[val]; !ok {
result[resultIndex] = val
resultIndex++
}
seen[val] = 1
}
return result[:resultIndex]
}
func main() {
// this won't work, see:
// https://groups.google.com/forum/#!msg/golang-nuts/Qwfovy0uGss/XoWCJo_sndEJ
strslice := []string{"hello", "hi", "hello"}
fmt.Printf("%v is unique\n", UniqueOf(strslice...))
}
/* Output
tim-schmelmers-macbook:go tim$ go run go_challenge_error.go
# command-line-arguments
./go_challenge_error.go:30: cannot use strslice (type []string) as type []interface {} in function argument
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment