I
I
Inter Carpenter2016-12-19 02:32:00
go
Inter Carpenter, 2016-12-19 02:32:00

How to make the program not terminate?

Hello
Tell me how to make a program in Golang that will not end?
You need to write a daemon, write waiting for the button to be pressed does not work when you add the program to the autorun
for{} at the end of the main() block somehow it’s not ethical for the
IDE to replace it with select{}
It certainly works, but somehow I don’t like this code, maybe is there something else?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kostyan Kondr, 2016-12-19
@kondr1

You can create a blocking channel at the end. So you decide when the program ends. It will run until it gets an empty struct from end

end := make(chan struct{})

<- end

R
Roman Kravchik, 2016-12-19
@rkravchik

It is necessary to specify the task.
If you want the program not to terminate, then you can use the os/signal construct with signals .
For example like this:

// exit program on ps kill and Ctrl+C...
  exitc := make(chan os.Signal, 1)
  signal.Notify(exitc, os.Interrupt, os.Kill, syscall.SIGTERM)
// some code or goroutines
// ...
  sig := <- exitc
// some cleanup or signal logging and printing

If you need to "daemonize" the application, check out the VividCortex/godaemon package .

I
index0h, 2016-12-19
@index0h

forever := make(chan bool)
<-forever

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question