I made a simple web app using Go. there is a goroutine
which is executed when user access a URL, let say /inspection/start/
. how to stop that goroutine
when user access URL /inspection/stop/
?
I heard about channel
but I am not sure how to do it in my case.
here is the code:
func inspection_form_handler(w http.ResponseWriter, r *http.Request) {
if r.FormValue("save") != "" {
airport_id := getCurrentAirportId(r)
r.ParseForm()
if airport_id != nil {
if r.FormValue("action") == "add"{
go read_serial_port()
}
// redirect back to the list
http.Redirect(w, r, "/airport#inspect", http.StatusSeeOther)
}
}
}
the routine function
func read_serial_port(){
c := &serial.Config{Name:"/dev/ttyACM0", Baud:9600}
s, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
filename:= randSeq(10)+".txt"
file, _ := os.Create("output/"+filename)
defer file.Close();
for{
buf := make([]byte, 128)
n, err := s.Read(buf)
if err != nil {
log.Fatal(err)
}
log.Printf("%s", string(buf[:n]))
fmt.Fprintf(file, string(buf[:n]))
time.Sleep(100 * time.Millisecond)
}
}