GoLand 代码优化:createTask 函数问题分析与改进

以下代码展示了 createTask 函数,其中存在一些潜在的问题和改进点:

func createTask(ctx context.Context, uin string, order *pb.OrderListDetails, returnOrderList *[]*pb.OrderListDetails) (func() error, string) {
	log.InfoContextf(ctx, '查询按消费明细开票信息-子任务, 订单号=%v', order.GetSubOrderNo())
	failedTsId := ''
	task := func() error {
		//预设置失败订单号,如无失败返回时设置为空
		failedTsId = order.GetSubOrderNo()
		// 请求云接口按照订单号获取订单的未开票金额、已开票金额
		GetInvoiceOrderListRsp, err := cloudBilling.GetInvoiceOrderList(ctx, uin, 1, 1, order.GetSubOrderNo())
		if err != nil {
			log.ErrorContextf(ctx, 'createTask qcloud.invoice.GetOrderInfoList.err, sub_order_no=%+v', order.GetSubOrderNo())
			return err
		}
		log.InfoContextf(ctx, 'GetInvoiceOrderList.Rsp=%+v', GetInvoiceOrderListRsp)
		log.InfoContextf(ctx, 'GetInvoiceOrderList.Rsp=%v', GetInvoiceOrderListRsp)
		if GetInvoiceOrderListRsp == nil {
			return errors.New('云接口数据异常,GetInvoiceOrderListRsp is nil')
		}
		if GetInvoiceOrderListRsp.ReturnCode != 0 {
			log.ErrorContextf(ctx, 'GetInvoiceOrderList.Rsp.err=%v', GetInvoiceOrderListRsp.ReturnMessage)
			return errors.New(GetInvoiceOrderListRsp.ReturnMessage)
		}
		log.InfoContextf(ctx, 'GetInvoiceOrderListRsp.Data.Detail')
		detailList := GetInvoiceOrderListRsp.Data.Detail
		if len(detailList) == 0 {
			return errors.New('云接口数据异常,订单数量为0')
		}
		if len(detailList) > 0 {
			returnOrder := &pb.OrderListDetails{}
			proto.Merge(returnOrder, order)

			log.InfoContextf(ctx, 'GetInvoiceOrderList len(detailList)')
			// 追加订单字段
			returnOrder.UnInvoiceAmt = proto.String(detailList[0].UnInvoiceAmount)
			returnOrder.InvoiceAmt = proto.String(detailList[0].InvoicedAmount)
			returnOrderList = append(returnOrderList, returnOrder)
			//无失败返回时设置为空
			failedTsId = ''
		}
		log.InfoContextf(ctx, 'order=%+v', order)
		log.InfoContextf(ctx, 'order=%v', order)
		return nil
	}
	log.InfoContextf(ctx, 'task=%+v, failedTsId=%v', task, failedTsId)
	return task, failedTsId
}

问题分析

  1. 函数返回的第二个参数 failedTsId 并没有被修改过,因此在函数体内的 failedTsId 赋值操作实际上没有必要。

  2. returnOrderList 是一个指针类型的切片,在函数内部对它进行操作时需要使用指针的方式,即 *returnOrderList。这里的 append 操作应该改为 *returnOrderList = append(*returnOrderList, returnOrder)

  3. 在处理错误时,函数应该返回错误类型的值,而不是使用 errors.New 创建一个新的错误。可以直接将 GetInvoiceOrderListRsp.ReturnMessage 作为错误信息返回。

  4. 在使用日志输出时,应该使用 %s 而不是 %v,因为日志输出的目的是文本信息,而不是变量的值。

改进方案

func createTask(ctx context.Context, uin string, order *pb.OrderListDetails, returnOrderList *[]*pb.OrderListDetails) (func() error, error) {
	log.InfoContextf(ctx, '查询按消费明细开票信息-子任务, 订单号=%s', order.GetSubOrderNo())
	task := func() error {
		// 请求云接口按照订单号获取订单的未开票金额、已开票金额
		GetInvoiceOrderListRsp, err := cloudBilling.GetInvoiceOrderList(ctx, uin, 1, 1, order.GetSubOrderNo())
		if err != nil {
			log.ErrorContextf(ctx, 'createTask qcloud.invoice.GetOrderInfoList.err, sub_order_no=%s: %s', order.GetSubOrderNo(), err)
			return err
		}
		log.InfoContextf(ctx, 'GetInvoiceOrderList.Rsp=%+v', GetInvoiceOrderListRsp)
		log.InfoContextf(ctx, 'GetInvoiceOrderList.Rsp=%s', GetInvoiceOrderListRsp)
		if GetInvoiceOrderListRsp == nil {
			return fmt.Errorf('云接口数据异常,GetInvoiceOrderListRsp is nil')
		}
		if GetInvoiceOrderListRsp.ReturnCode != 0 {
			errMsg := fmt.Sprintf('GetInvoiceOrderList.Rsp.err=%s', GetInvoiceOrderListRsp.ReturnMessage)
			log.ErrorContextf(ctx, errMsg)
			return fmt.Errorf(errMsg)
		}
		log.InfoContextf(ctx, 'GetInvoiceOrderListRsp.Data.Detail')
		detailList := GetInvoiceOrderListRsp.Data.Detail
		if len(detailList) == 0 {
			return fmt.Errorf('云接口数据异常,订单数量为0')
		}
		if len(detailList) > 0 {
			returnOrder := &pb.OrderListDetails{}
			proto.Merge(returnOrder, order)

			log.InfoContextf(ctx, 'GetInvoiceOrderList len(detailList)')
			// 追加订单字段
			returnOrder.UnInvoiceAmt = proto.String(detailList[0].UnInvoiceAmount)
			returnOrder.InvoiceAmt = proto.String(detailList[0].InvoicedAmount)
			*returnOrderList = append(*returnOrderList, returnOrder)
		}
		log.InfoContextf(ctx, 'order=%+v', order)
		log.InfoContextf(ctx, 'order=%s', order)
		return nil
	}
	log.InfoContextf(ctx, 'task=%+v', task)
	return task, nil
}

总结

通过对代码的分析和优化,我们提升了 createTask 函数的健壮性和可读性。改进后的代码更加规范,错误处理更加合理,日志输出也更加清晰。开发者在编写代码时,应该注意这些细节,以保证代码质量和可维护性。

GoLand 代码优化:`createTask` 函数问题分析与改进

原文地址: https://www.cveoy.top/t/topic/oBGO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录