doukougua7873 2018-07-26 09:06
浏览 531
已采纳

Golang从..到读取字节数组并显示结果

First of all: I know that this is super basic question and you'd expect to find enough material on the internet and there probably is. I feel pretty stupid right now for not understanding it, so no need to point that out to me - I know^^ From the google Directory API, the response you get when reading a custom Schema is JSON-enocded: https://developers.google.com/admin-sdk/directory/v1/reference/schemas I copy/pasted that response and wanted to read it.

 func main() {
    jsonExample := `
    {
  "kind": "admin#directory#schema",
  "schemaId": "string",
  "etag": "etag",
  "schemaName": "string",
  "displayName": "string",
  "fields": [
    {
      "kind": "admin#directory#schema#fieldspec",
      "fieldId": "string",
      "etag": "etag",
      "fieldType": "string",
      "fieldName": "string",
      "displayName": "string",
      "multiValued": true,
      "readAccessType": "string",
      "indexed": true,
      "numericIndexingSpec": {
        "minValue": 2.0,
        "maxValue": 3.0
      }
    }
  ]
}
`

    var jsonDec schemaExample

    jsonExampleBytes := []byte(jsonExample)    

    m := make(map[string]interface{})
    err := json.Unmarshal([]byte(jsonExample), &m)
    byteStorage := make([]byte,600)
        byteReader := bytes.NewReader(byteStorage)
res, err := byteReader.ReadAt(jsonExampleBytes,50)
fmt.Printf("############Hier : %v Err: 
%v",res,err)
fmt.Printf("Storage: %v
",byteStorage)



byteStorage := make([]byte,600)
byteReader := bytes.NewReader(byteStorage)
        res, err := byteReader.ReadAt(jsonExampleBytes,50)
        fmt.Printf("Result : %v Err: %v
",res,err)
        fmt.Printf("Storage: %v
",byteStorage)

This returns

res : 526 Err: <nil>
Storage: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

.My question is how to implement a ReadFromTo method, which allows me to read a specific range of bytes from a byte array? And since the storage is empty, I also lack to understand how read that array back at all with the reader functions, only way I know how to pull it off is this:

fmt.Printf("Und die bytes to String: %v",string([]byte(jsonExample)))
  • 写回答

2条回答 默认 最新

  • dongyou5098 2018-07-26 09:48
    关注

    From the docs (emphasis mine):

    ReaderAt is the interface that wraps the basic ReadAt method.

    ReadAt reads len(p) bytes into p starting at offset off in the underlying input source.

    type ReaderAt interface {
        ReadAt(p []byte, off int64) (n int, err error)
    }
    

    The argument to ReadAt (and Read in general) is the destination. You've got jsonExampleBytes and byteStorage the wrong way around.

    package main
    
    import (
            "bytes"
            "fmt"
    )
    
    func main() {
            jsonExampleBytes := []byte(`{...}`)
    
            byteReader := bytes.NewReader(jsonExampleBytes)
    
            byteStorage := make([]byte, 600)
            n, err := byteReader.ReadAt(byteStorage, 3)
    
            fmt.Println("Storage:", string(byteStorage[:n]), err) // Storage: .} EOF
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • doume5227 2018-07-26 09:48
    关注

    To access a sub-slice of bytes, you can in the most basic case just use the index operator:

    array := make([]byte, 100)
    bytes5to9 = array[5:10]
    

    note here that the second index is exclusive.

    If you need an io.Reader from these bytes, you can use

    r := bytes.NewReader(array[5:10])
    

    You can do this again, creating a second read for the same or a different range of the array.

    The utility functions in io and ioutil might be of interest to you as well. See for example ioutil.ReadAll, io.Copy, io.CopyBuffer, io.CopyN and io.ReadFull.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DEA的CCR模型画图
  • ¥15 请假各位一个关于安卓车机的问题
  • ¥15 光谱仪怎么看这样的透射谱
  • ¥15 pyqt5 如何实现输入框输入关键词,下拉框显示模糊查询返回的结果?
  • ¥20 fluent模拟,可以燃烧和相变同时模拟吗?
  • ¥50 海康摄像头,C#如何识别车牌号码和抓取JPG
  • ¥15 yolov5 pt转engine的问题
  • ¥15 一公司的网络工程设计
  • ¥15 windows11蓝屏
  • ¥100 有没有在EVE虚拟机中使用的神州数码DCN的网络设备镜像文件