【问题标题】:Golang : Byte insert into [ ] byteGolang:字节插入[]字节
【发布时间】:2019-05-23 15:18:31
【问题描述】:

我正在处理 GRPC 流,在服务器端,我在 for 循环中接收到多个字节,我想合并到一个字节数组中(我尝试了 append 方法但没有使用),在这里我附上了我的示例代码。任何人指导我。

示例代码

func (s *ServerGRPC) Upload(stream pb.GuploadService_UploadServer) (err error) {

for {
        resp, err := stream.Recv()

        if err != nil {
            if err == io.EOF {
                goto END
            }

            err = errors.Wrapf(err,
                "failed unexpectadely while reading chunks from stream")
            return err
        }

        for _, result := range resp.Content {

            fmt.Println("result ====>>>", result)

          //Actual Output
          //result ====>>> 136
          //result ====>>> 84
          //result ====>>> 232
          //result ====>>> 12

          //Expectation
          //result ===> [136 84 232 12] 

        }

    }
        s.logger.Info().Msg("upload received")

    END:
        err = stream.SendAndClose(&pb.UploadStatus{
            Message: "Upload received with success",
            Code:    pb.UploadStatusCode_Ok,
        })
        if err != nil {
            err = errors.Wrapf(err,
                "failed to send status code")
            return
        }

        return
    }

【问题讨论】:

    标签: arrays for-loop go grpc


    【解决方案1】:
    func (s *ServerGRPC) Upload(stream pb.GuploadService_UploadServer) (err error) {
    
    var respBytes []byte
    for {
            resp, err := stream.Recv()
    
            if err != nil {
                if err == io.EOF {
                    goto END // you can use break here
                }
    
                err = errors.Wrapf(err,
                    "failed unexpectadely while reading chunks from stream")
                return err
            }
    
            for _, result := range resp.Content {
    
                fmt.Println("result ====>>>", result)
                respBytes = append(respBytes, result)
    
            }
        }
            s.logger.Info().Msg("upload received")
            // print respBytes here
        END:
            err = stream.SendAndClose(&pb.UploadStatus{
                Message: "Upload received with success",
                Code:    pb.UploadStatusCode_Ok,
            })
            if err != nil {
                err = errors.Wrapf(err,
                    "failed to send status code")
                return
            }
    
            return
        }
    

    【讨论】:

    • []bytes 或 []byte..?,如果 []bytes 表示它显示未定义,如果我使用 []byte 它追加方法需要更多时间并且虚拟机挂起。
    • 它的 [] 字节。感谢您指出。虚拟机是挂了还是在等待响应?
    • 追加进程未完成,需要更多时间,然后vm挂了。
    • 从 grpc 服务返回的响应的大小是多少?您为 VM 分配了多少内存?
    • Vm 内存为 6GB,结果总大小为 6000 之类的,所以每次迭代结果都会被追加,因此需要最长时间。
    【解决方案2】:

    您正在附加到 Sample 但打印 req.Content
    合并切片没有任何问题。如果只需要打印 Sample 即可查看合并结果。

    【讨论】:

    • 我做了一些更改,请在我的代码中检查并告诉我。
    • 你现在在哪里连接切片??
    猜你喜欢
    • 2012-10-18
    • 2022-12-03
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多