go例程完成后的Golang结束功能

时间:2020-07-03 21:08:15

标签: go

我有一个看起来像这样的主要功能:


Widget sliverGridWidget(BuildContext context, List<Photos> listPhotoDetail){
    return StaggeredGridView.countBuilder(
        padding: const EdgeInsets.all(8.0),
        crossAxisCount: view?2:6, //if view true make it vertical view otherwise grid view
        itemCount: listPhotoDetail.length,
        itemBuilder: (context, index){
          
          return InkWell(
            onLongPress: (){
              setState(() {
                enable = true;
              });
            },
           
              child: Container(
             
              decoration: BoxDecoration(
                color:Colors.grey[100],

                image: DecorationImage(
                  image: NetworkImage(listPhotoDetail[index].photoDetail.image.fileUrl),
                  fit: BoxFit.cover
                )
              ),
              child:enable? Stack(
                children: <Widget>[
                  Positioned(
                    bottom: 5,
                    right: 3,
                    child: InkWell(
                     child:  listPhotoDetail[index].isSelected
                      ?Image.asset('assets/icons/selected.png') 
                      
                      :Image.asset('assets/icons/select_on_image.png'),
                      onTap: (){
                         setState(() {
                            listPhotoDetail[index].isSelected = !listPhotoDetail[index].isSelected;
                              count= listPhotoDetail[index].isSelected ?count+1:count-1;
                               if(listPhotoDetail[index].isSelected){
                                  selectedPhotostList.add(
                                    Photos(
                                      photoDetail:listPhotoDetail[index].photoDetail ,
                                      //  contact:contactList[index].contact ,
                                      isSelected:listPhotoDetail[index].isSelected) 
                                    
                                    );
                                 }  else{
                                  selectedPhotostList.removeAt(index);
                                 } 
                           
                            // listPhotoDetail[index].isSelected = ! listPhotoDetail[index].isSelected;
                            // count =  listPhotoDetail[index].isSelected ?count+1:count-1;
                         });
                      }, 
                    )
                  )
                ],
              )
              :Container()
            ),
          );
        },
         staggeredTileBuilder: (index) 
         => StaggeredTile.count(2, 2), //Make size as you want.
         mainAxisSpacing: 8.0,
         crossAxisSpacing:8.0,
      );
  }
}

这两个功能应该无限期地继续。我想func main() { go SyncRealTime() go SyncStale() }

  • 只要两个Goroutine都在运行,就不会终止。
  • 如果任一Goroutine终止(即错误),则终止

在Go中惯用的方式是什么?

1 个答案:

答案 0 :(得分:1)

这是一种实现方式:

func main() {
    c := make(chan string)
    go func() {
        SyncRealTime()
        c <- "SyncRealTime"
    }()

    go func() {
        SyncStale()
        c <- "SyncStale"
    }()

    firstDone := <-c
    fmt.Println(firstDone + " exited")
    // done
}