Passing a refference into a function

I need to pass a refference of slice into a function but something is wrong because iterating over slice doesn’t output any data.

package main

import (

func main() {

	timeMarkers := []string{}

	go timeMarkersSearch(&timeMarkers)

	for _, marker := range timeMarkers {


func timeMarkersSearch(timeMarkers *[]string) {
	s1 := "s1"
	s2 := "s2"
	*timeMarkers = append(*timeMarkers, s1)
	*timeMarkers = append(*timeMarkers, s2)

There is nothing wrong, but main prints the empty slice before the goroutine is able to add any elements.

You can either remove the go or use a sync.WaitGroup to synchronize your go routines as I did on the playground


You don’t need to use sync.WaitGroup for that. You can simply use a done channel. Here’s the refactored code.

I also think that you may want to use channels instead, like this.


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