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

标签 arrays go io

首先:我知道这是一个 super 基础的问题,您会希望在互联网上找到足够的资料,而且很可能有。我现在因为不理解它而感到很愚蠢,所以不需要向我指出这一点 - 我知道^^ 从 google Directory API 中,您在读取自定义架构时获得的响应是​​ JSON-enocded: https://developers.google.com/admin-sdk/directory/v1/reference/schemas 我复制/粘贴了该回复并想阅读它。

 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: \n%v",res,err)
fmt.Printf("Storage: %v\n",byteStorage)



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

返回

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]

。我的问题是如何实现 ReadFromTo 方法,它允许我从字节数组中读取特定范围的字节?而且由于存储空间是空的,我也不知道如何使用读取器函数读回该数组,我知道如何实现它的唯一方法是:

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

最佳答案

来自文档(强调我的):

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)
}

ReadAt(和一般的 Read)的参数是目的地。您以错误的方式获得了 jsonExampleBytes 和 byteStorage。

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
}

关于arrays - Golang 从 .. 读取字节数组并显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51535047/

相关文章:

javascript - 使用 d3.js 在表中显示关联数组的数据

go - 在 Go 中访问 map 接口(interface)值

python - 如何计算具有由索引数组定义的不同窗口大小的 NumPy 数组的移动平均值?

C++ 位列表作为数字

python - 将大量数据存储到 numpy 数组中

windows - Golang exec .bat 或 .exe 文件但没有得到输出

json - 解码到相同的结构但不同的 json 名称

Ruby IO#read 单次读取的最大长度

java - 为复制的文件生成文件名

java - Java中Scanner的构建和使用