go chan 扇入扇出模式

试卷阅卷:
(工序1)准备题目数据->(工序2)题目处理->(工序3)试卷报告

在阅卷操作运转后,发现题目处理较耗费时间,导致工序1和工序3也相应的慢了下来,为了提升性能,题目处理增加了两个人手:

type Question struct {
	No         int     // 题目编号
	Type       int     // 题型
	Ans        string  // 题目答案
	StudentAns string  // 学生答案
	Score      float32 // 分数
	IsRgiht    bool    // 对/错
}

// 题目数据
var QuestionData = map[int]Question{
	1001: {Ans: "A", Score: 10},
	1002: {Ans: "D", Score: 10},
	2001: {Ans: "C", Score: 10},
	2002: {Ans: "B", Score: 10},
	3001: {Ans: "A", Score: 10},
	3002: {Ans: "D", Score: 10},
	4001: {Ans: "A", Score: 10},
	4002: {Ans: "C", Score: 10},
	5001: {Ans: "D", Score: 10},
	5002: {Ans: "B", Score: 10},
}

func TestExam(t *testing.T) {
	//试卷
	var exam = map[int][]Question{
		1: {{No: 1001, StudentAns: "A", Type: 1}, {No: 1002, StudentAns: "D"}},
		2: {{No: 2001, StudentAns: "C", Type: 2}, {No: 2002, StudentAns: "B"}},
		3: {{No: 3001, StudentAns: "A", Type: 3}, {No: 3002, StudentAns: "D"}},
		4: {{No: 4001, StudentAns: "A", Type: 4}, {No: 4002, StudentAns: "C"}},
		5: {{No: 5001, StudentAns: "D", Type: 5}, {No: 5002, StudentAns: "B"}},
	}
	//开始阅卷 Start
	q := FindQuestion(exam)
	qRes1 := Scoring(q)
	qRes2 := Scoring(q)
	qRes3 := Scoring(q)
	_, score := report(qRes1, qRes2, qRes3)
	//开始阅卷 End
	fmt.Println("得分:", score)
	//得分: 100
}

// 查询试卷题目信息
func FindQuestion(exam map[int][]Question) <-chan []Question {
	out := make(chan []Question)
	go func() {
		defer close(out)
		for key := range exam {
			for k := range exam[key] {
				exam[key][k].Ans = QuestionData[exam[key][k].No].Ans
			}
			out <- exam[key]
		}
	}()
	return out
}

// 题目阅卷处理
func Scoring(in <-chan []Question) <-chan []Question {
	out := make(chan []Question)
	go func() {
		defer close(out)
		for c := range in {
			for k, question := range c {
				//处理学生题目数据
				if question.StudentAns == question.Ans {
					c[k].IsRgiht = true
					c[k].Score = QuestionData[question.No].Score
				} else {
					c[k].IsRgiht = false
				}
			}
			//其他操作。。。
			out <- c
		}
	}()
	return out
}

// 生成试卷报告
func report(ch ...<-chan []Question) (m map[int][]Question, score float32) {
	var wg sync.WaitGroup
	wg.Add(len(ch))
	m = make(map[int][]Question)
	p := func(c <-chan []Question) {
		defer wg.Done()
		for questions := range c {
			m[questions[0].Type] = questions
			for _, q := range questions {
				score += q.Score
			}
		}
	}
	for _, c := range ch {
		go p(c)
	}
	wg.Wait()
	return
}

posted @ 2023-04-10 15:54  耳东01  阅读(23)  评论(0)    收藏  举报