Skip to main content

How to terminate goroutines in golang

· One min read
Shekhar Patil
Full stack developer.

How to terminate goroutines in golang?

Method 1: Using Channels to Signal Termination

package main

import (
"fmt"
"time"
)

func performTask(terminateChan <-chan bool) {
for {
select {
case <-terminateChan:
fmt.Println("Task terminated by channel")
return
default:
fmt.Println("Task is running")
time.Sleep(time.Second)
}
}
}

func main() {
terminateChan := make(chan bool)

go performTask(terminateChan)

time.Sleep(5 * time.Second)
terminateChan <- true
time.Sleep(time.Second)
fmt.Println("Main Goroutine exited")
}