本文整理汇总了Golang中encoding/json.Compact函数的典型用法代码### 示例。如果您正苦于以下问题:Golang Compact函数的具体用法?Golang Compact怎么用?Golang Compact使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了Compact函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: equaljson

func equaljson(p, q []byte) bool {
    cp := bytes.NewBuffer([]byte{})

    if err := json.Compact(cp, p); err != nil {
        log.Printf("unable to compact cp json for equaljson: %+v", err)
        return false
    }

    cq := bytes.NewBuffer([]byte{})

    if err := json.Compact(cq, q); err != nil {
        log.Printf("unable to compact cq json for equaljson: %+v", err)
        return false
    }

    if len(cp.Bytes()) != len(cq.Bytes()) {
        return false
    }

    cpb, cqb := cp.Bytes(), cq.Bytes()

    for i, b := range cpb {
        if b != cqb[i] {
            return false
        }
    }

    return true
}

开发者ID:wkharold,项目名称:uber-apps,代码行数:29,代码来源:main_test.go

示例2: processJson

func processJson(filename string, src []byte, opt *JsonFmtOption) ([]byte, error) {
    if opt.Compact {
        var out bytes.Buffer
        err := json.Compact(&out, src)
        if err != nil {
            return nil, err
        }
        return out.Bytes(), nil
    } else {
        var out bytes.Buffer
        var err error
        if opt.IndentTab {
            err = json.Indent(&out, src, "", "\t")
        } else {
            var indent string
            for i := 0; i < opt.TabWidth; i++ {
                indent += " "
            }
            err = json.Indent(&out, src, "", indent)
        }
        if err != nil {
            return nil, err
        }
        return out.Bytes(), nil
    }
    return src, nil
}

开发者ID:ewindfall,项目名称:go-liteide-tools,代码行数:27,代码来源:jsonfmt.go

示例3: pathRolesWrite

func pathRolesWrite(
    req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
    var buf bytes.Buffer

    uip, err := useInlinePolicy(d)
    if err != nil {
        return nil, err
    }

    if uip {
        if err := json.Compact(&buf, []byte(d.Get("policy").(string))); err != nil {
            return logical.ErrorResponse(fmt.Sprintf(
                "Error compacting policy: %s", err)), nil
        }
        // Write the policy into storage
        err := req.Storage.Put(&logical.StorageEntry{
            Key:   "policy/" + d.Get("name").(string),
            Value: buf.Bytes(),
        })
        if err != nil {
            return nil, err
        }
    } else {
        // Write the arn ref into storage
        err := req.Storage.Put(&logical.StorageEntry{
            Key:   "policy/" + d.Get("name").(string),
            Value: []byte(d.Get("arn").(string)),
        })
        if err != nil {
            return nil, err
        }
    }

    return nil, nil
}

开发者ID:GauntletWizard,项目名称:vault,代码行数:35,代码来源:path_roles.go

示例4: readFiles

func readFiles(files ...string) []byte {
    lfs := len(files)
    chContent := make(chan []byte, lfs)

    for _, file := range files {
        go func(chContent chan []byte, configPath string) {
            chContent <- format(configPath)
        }(chContent, file)
    }

    bytess := make([][]byte, 0, lfs)

    for i := 1; i <= lfs; i++ {
        content := <-chContent

        if len(content) != 0 {
            bytess = append(bytess, content)
        }
    }

    buf := bytes.NewBufferString(`{`)
    buf.Write(bytes.Join(bytess, []byte(`,`)))
    buf.WriteString(`}`)

    var contentBuf bytes.Buffer
    err := json.Compact(&contentBuf, buf.Bytes())
    if err != nil {
        log.Debug("<readFiles> jsonData: ", buf.String())
        log.Fatal("<readFiles> error: ", err)
    }

    return contentBuf.Bytes()
}

开发者ID:golangers,项目名称:config,代码行数:33,代码来源:config.go

示例5: prepAndPostEvent

func prepAndPostEvent(eventFile string, preFunc PreFunc) (err error) {
    rawEvent, err := ioutil.ReadFile(eventFile)
    if err != nil {
        return err
    }

    event := &Event{}
    err = json.Unmarshal(rawEvent, &event)
    if err != nil {
        return err
    }
    preFunc(event)
    rawEvent, err = json.Marshal(event)
    if err != nil {
        return err
    }

    buffer := new(bytes.Buffer)
    err = json.Compact(buffer, rawEvent)
    if err != nil {
        return err
    }
    http.Post(pushUrl, "application/json", buffer)

    return nil
}

开发者ID:th3architect,项目名称:go-machine-service,代码行数:26,代码来源:listener_test.go

最后编辑: kuteng  文档更新时间: 2021-08-23 19:14   作者:kuteng