Last active
December 27, 2015 04:39
-
-
Save timbogit/7268248 to your computer and use it in GitHub Desktop.
Variadic function of interfaces called with a string slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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