You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: worker.go
    path of file: ./repos/pan-light/pc/downloader
    
    the code of the file until where you have to start completion: package downloader

import (
	"context"
	"fmt"
	"github.com/pkg/errors"
	"io"
	"log"
	"net/http"
)

// 实际下载协程
type worker struct {
	id     int
	task   *Task
	cancel func()
	ctx    context.Context
}

func (w *worker) work() {
	w.ctx, w.cancel = context.WithCancel(context.Background())
	errorNumber := 0
	maxErrorNumber := 2
	// 循环下载片段
WorkLoop:
	for {

		// 检查是否有连续错误
		if errorNumber > maxErrorNumber {
			log.Println("too many errors occurred")
			break
		}

		// 判断是否被取消
		select {
		case <-w.ctx.Done():
			break WorkLoop
		default:
		}

		// 获取新的下载片段
		seg, err := w.task.distributeSegment()
		// 没有新的下载片段
    
func (w *worker) work() {
	w.ctx, w.cancel = context.WithCancel(context.Background())
	errorNumber := 0
	maxErrorNumber := 2
	// 循环下载片段
WorkLoop:
	for {

		// 检查是否有连续错误
		if errorNumber > maxErrorNumber {
			log.Println("too many errors occurred")
			break
		}

		// 判断是否被取消
		select {
		case <-w.ctx.Done():
			break WorkLoop
		default:
		}

		// 获取新的下载片段
		seg, err := w.task.distributeSegment()
		// 没有新的下载片段, 退出下载循环
		if err == noMoreSeg {
			break
		}
		// 错误计数
		if err != nil {
			errorNumber++
			continue
		}
		// 下载该片段
		err = w.downloadSeg(seg)
		// 下载出错, 放回队列
		if err != nil {
			log.Println(err)
			w.task.downloadSegmentError(seg)
			errorNumber++
			continue
		}
		// 本次下载没出错, 错误计数置零
		errorNumber = 0
		w.task.downloadSegmentSuccess(seg)
	}
}