How to pass any datatype variable to the function

I was trying to create a function that can take any type of datatype.
Here I will pass the variable which has contents but not the content directly.

func Anytype(buffer interface{}) error {
	switch d := buffer.(type) {
	case string:
		fmt.Println("String")
	case []byte:
              fmt.Println("byte array")
	}
	return nil
}
func main(){
a:="string"
b:=[]byte{1,23,4}
Anytype(a)
Anytype(b)
}

But when I run this I am getting this error.How to solve this.

cannot convert str (type interface {}) to type string: need type assertion

Thanks

Tested in playground your program have no errors :thinking:

https://play.golang.org/p/kYqMF8VaMfF

Now Its working fine:upside_down_face:

Thanks

That message does not match your code. You do not have a variable str.

I generalized the code so that it will be easy to understand.
But copied the error from the main code. Sorry

Thanks

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.