Arrays Golang从..读取字节数组。。查看并显示结果

Arrays Golang从..读取字节数组。。查看并显示结果,arrays,go,io,Arrays,Go,Io,首先:我知道这是一个超级基本的问题,你会期望在互联网上找到足够的资料,而且可能有。我现在觉得自己很愚蠢,因为我不理解它,所以没必要向我指出这一点——我知道^^ 从google Directory API中,读取自定义模式时得到的响应是JSON enocded: 我复制/粘贴了该响应,并想阅读它 func main() { jsonExample := ` { "kind": "admin#directory#schema", "schemaId": "string",

首先:我知道这是一个超级基本的问题,你会期望在互联网上找到足够的资料,而且可能有。我现在觉得自己很愚蠢,因为我不理解它,所以没必要向我指出这一点——我知道^^ 从google Directory API中,读取自定义模式时得到的响应是JSON enocded: 我复制/粘贴了该响应,并想阅读它

 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 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

要访问字节的子片,在最基本的情况下,只需使用索引运算符:

array := make([]byte, 100)
bytes5to9 = array[5:10]
请注意,第二个索引是独占的

如果需要这些字节中的
io.Reader
,可以使用

r := bytes.NewReader(array[5:10])
您可以再次执行此操作,为相同或不同范围的数组创建第二次读取


io
ioutil
中的实用程序函数可能也会引起您的兴趣。例如,请参见、、和。

要访问字节的子片,在最基本的情况下,只需使用索引运算符即可:

array := make([]byte, 100)
bytes5to9 = array[5:10]
请注意,第二个索引是独占的

如果需要这些字节中的
io.Reader
,可以使用

r := bytes.NewReader(array[5:10])
您可以再次执行此操作,为相同或不同范围的数组创建第二次读取

io
ioutil
中的实用程序函数可能也会引起您的兴趣。例如,请参见文档中的、、和。

(重点):

ReaderAt是封装基本ReadAt方法的接口

ReadAt从底层输入源中的offset off开始,将len(p)字节读入p

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
}
从文档(我的重点):

ReaderAt是封装基本ReadAt方法的接口

ReadAt从底层输入源中的offset off开始,将len(p)字节读入p

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
}

这不是有效的json。它显示的是值的类型,而不是值本身。谢谢,我更改了它,现在这些错误都消失了——剩下的问题仍在思考中。这不是一个有效的json。它显示的是值的类型,而不是值本身。谢谢,我更改了它,现在这些错误都消失了——剩下的问题仍然在思考中。