该报错是因为使用了反射函数 reflect.Value.Interface() 获取了一个未导出的字段或方法的值。在 Go 语言中,未导出的字段或方法只能在其定义所在的包中访问,无法被外部包访问。因此,如果在外部包中使用反射函数获取未导出的字段或方法的值,就会产生该报错。

在上述代码中,可能是在调用 proto.Merge() 函数时使用了反射函数导致了该报错。

// batchGetOrderInfoList 批量请求云rpc接口,控制并发数N
func batchGetOrderInfoList(ctx context.Context, uin string, orders []*pb.OrderListDetails,
	returnOrderList *[]*pb.OrderListDetails) ([]string, error) {
	failedTsIds := make([]string, 0)
	tasks := make([]func() error, 0)

	for i := 0; i < len(orders); i++ {
		order := orders[i]
		// 创建任务
		task, failedTsId := createTask(ctx, uin, order, returnOrderList)
		tasks = append(tasks, task)
		if failedTsId != ""
			failedTsIds = append(failedTsIds, failedTsId)
	}
	return failedTsIds, util.GoAndWait(10, tasks...)
}

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 = detailList[0].UnInvoiceAmount
			*returnOrder.InvoiceAmt = 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
}
GoLand 开发中 reflect.Value.Interface 报错:无法返回从未导出字段或方法获取的值

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

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