doucheng4660 2018-06-25 07:47
浏览 208

当我添加multipart.NewWriter(body_buf)时,程序不会停止

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

  • 写回答

1条回答 默认 最新

  • dousuochu7291 2018-06-25 08:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答