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

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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 提个关于sql的问题
      • ¥15 Python库一直装不好
      • ¥30 在linux上调用海康SDK没有进入函数内
      • ¥15 FreeRTOS有任务卡死
      • ¥15 vue网页地址中的#问题
      • ¥20 一个js里的函数的retur值想返回另一个js的变量值,应该怎么写?
      • ¥15 登陆器jar2exe过期了怎么样重新授权
      • ¥15 我的电脑安装好的PS总是会自己消失,请问这是为什么?
      • ¥15 这个程序哪里有问题呢,显示0分
      • ¥15 我想问一下像图片中这种效果怎么实现