By André Betz


2018-08-21 03:23:07 8 Comments

I would like to know how to kill/stop goroutines. All examples are based on channels and select, which seems to only work if the goroutine contains some repeating task between which it can listen on a channel. Is there a way to stop the below goroutine before it returns?

package main

import (
        "time"
)

func main() {
    stop := make(chan string, 1)

    go func() {
        time.Sleep(10 * time.Second)
        stop <- "stop"
        return
    }()
    <- stop
}

1 comments

@Volker 2018-08-21 03:57:09

Is there a way to stop the below goroutine before it returns?

No there is not (except calling os.Exit to abort the whole program).

Goroutines are self-contained and not controllable from the outside.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How can i Await a Single Goroutine Response on Multiple other Goroutines?

  • 2019-02-24 16:50:40
  • Sabri Boughanmi
  • 60 View
  • 0 Score
  • 1 Answer
  • Tags:   go goroutine

1 Answered Questions

[SOLVED] How to stop a goroutine if we fail to listen from a channel passed to that routine

  • 2019-02-01 08:22:53
  • Rahul Kushwaha
  • 52 View
  • -1 Score
  • 1 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Signal goroutines to stop with channel close

  • 2018-11-12 21:43:50
  • Chen A.
  • 627 View
  • 1 Score
  • 1 Answer
  • Tags:   go channel

2 Answered Questions

[SOLVED] How to stop a goroutine that is listening for RethinkDB changefeeds?

1 Answered Questions

[SOLVED] How to wait for a group of goroutines?

1 Answered Questions

[SOLVED] How to stop goroutine blocked by external I/O started for process?

1 Answered Questions

[SOLVED] Is main just a normal goroutine?

3 Answered Questions

[SOLVED] How do I tell my test to wait for a callback in a goroutine?

  • 2015-07-31 15:39:31
  • deitch
  • 2060 View
  • 0 Score
  • 3 Answer
  • Tags:   go goroutine

1 Answered Questions

[SOLVED] producer consumer in golang

  • 2015-04-14 18:18:57
  • srock
  • 4130 View
  • 2 Score
  • 1 Answer
  • Tags:   go goroutine

1 Answered Questions

[SOLVED] Different results for N>1 goroutines (on N>1 Cpu:s). Why?

Sponsored Content