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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置