Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i create an array that can hold multiple data types in GoLang?

Tags:

arrays

go

While declaring an array or a slice I'm required to specify the type of data that I want to store in the array or list.

How do I make an array that can hold data of multiple types ?

like image 610
Daksh Avatar asked Oct 24 '25 08:10

Daksh


1 Answers

You can do something like this mixedArray := []interface{}{"astring", 10, &Object{'hello'}}

Most likely the values that you want to hold share some behavior, in that case what you should really do is create an interface and simply make the array content type that interface.

like image 126
Ezio Avatar answered Oct 26 '25 23:10

Ezio