步骤说明

// 定义匿名函数方式一
add := func (x, y int) {
fmt.Println(x + y)
}
add(1, 2) // 通过变量调用匿名函数

// 定义匿名函数方式二
func (x, y int) {
fmt.Println(x - y)
}(20, 5) //自执行函数:匿名函数定义完加()直接执行
// TaskSyncUpload上传图片
func TaskSyncBatchUpload(ctx *gin.Context) {
type Params struct {
MemberId int64 `json:"member_id"`
FormData []*model.MemberTaskFormData `json:"form_data"`
}

var params Params
_ = ctx.BindJSON(&params)

ch := make(chan bool, len(params.FormData))
for _, v := range params.FormData {
if v.FormType == "IMG" {
go func(downloadUrl string) {
ch <- svc.DownloadFile("", "./", downloadUrl)
}(v.FormValue)
}
}

// 等待每个文件下载的完成,并检查超时
timeout := time.After(600 * time.Second)
for idx := 0; idx < len(params.FormData); idx++ {
select {
case res := <-ch:
nt := time.Now().Format("2006-01-02 15:04:05")
fmt.Printf("[%s]Finish download %s\n", nt, strconv.FormatBool(res))
case <-timeout:
fmt.Println("Timeout...")
break
}
}

resp.Success(ctx, resp.Response{})
}