How to use callbacks in golang?


(shubham jain) #1

I need to use go threads infinite time.


(Johan Dahl) #2

Hi. I’m not really sure what you want to accomplish. Could you be more specific?


(Inanc Gumus) #3

There isn’t any “go threads” in Go, there are “Go Routines”.

To terminate a single goroutine just send it a signal, like this:

package main

import (
	"fmt"
	"time"
)

func main() {
	signal := make(chan struct{})
	go func() {
		fmt.Println("waiting")
		<-signal
	}()

	// wait for 5 secs then send the signal
	time.Sleep(time.Second * 5)

	// close will unblock every receiver
	fmt.Println("sending the signal")
	close(signal)
}