how to compare contain of two Slices (arrays) and display the elements that boths sharing.

como comparar el contenido de 2 Slices y mostrar los elemontos que se encuentran en ambos slices.

package main
import "fmt"

// compares two slices and returns matches elements 
func compare(a, b []int) []int {
// create empty struct
type void struct{}

// create map with length of the 'a' slice
myMap := make(map[int]void, len(a))
// create slice to collect matches
matches := []int{}

// Convert first slice to map with empty struct 
for _, ka := range a {
myMap[ka] = void{}
// find matching values in a
for _, kb := range b {
if _, ok := myMap[kb]; ok {
matches = append(matches, kb)
return matches

func main() {
// example Slices X and Y
x := []int{1,2,3,4,5}
y := []int{5,6,7,8,9}

// Display matching elements in Slice X and Slice Y