package main
import (
"fmt"
"mime/multipart"
"bytes"
)
var channel chan string = make(chan string)
func recognize(file_path string) {
body_buf := &bytes.Buffer{}
fmt.Println(body_buf)
send_writer := multipart.NewWriter(body_buf)
fmt.Println(send_writer)
}
func loop() {
for i := 0; i < 10; i++ {
channel <- "dd"
}
}
func main() {
go loop()
for v := range channel {
fmt.Println(len(channel), v)
}
}
the program will not stop, even i don't call the recognize
function, i don't know why, how to explain that
when i remove the
send_writer := multipart.NewWriter(body_buf)
the program will stop and get fatal error: deadlock
what make it difference, who can tell me
The program will not stop as you never close the channel and thus the range loop over it will not terminate. Close the channel in loop
like
func loop() {
for i := 0; i < 10; i++ {
channel <- "dd"
}
close(channel)
}
and it should stop.