代码之家  ›  专栏  ›  技术社区  ›  thisguy123

无法从字节数组中删除空终止符?

go
  •  0
  • thisguy123  · 技术社区  · 6 年前

    我在用 this library 在go中(在osx上)与windows dns服务器交互。

    当运行下面的代码片段时,我得到一个关于空终止符的错误。

     $ ~/go/bin/winrm-dns-client create -d domain.com -n node-0 -t A -v 10.90.61.30
    2018/06/03 12:40:22 Error creating DNS record: Reading record: Reading record: Unmarshalling response: Unmarshalling json: invalid character '\x00' after array element
    

    我怀疑有一个无效的终结者 here 当helper方法调用 sprintf 将json响应连接到数组中。

    但是,即使在添加了 bytes.Trim 如下图所示…我仍然得到一个空终止符错误,似乎空终止符仍然存在…

    func unmarshalResponse(resp string) ([]interface{}, error) {
        var data interface{}
        byteRespTrim := []byte(resp)
        fmt.Print("found a null terminator at -- ")
        fmt.Println(bytes.Index(byteRespTrim, []byte("\x00")))
        fmt.Print("total length = ")
        fmt.Println(len(byteRespTrim))
        byteRespTrim = bytes.Trim(byteRespTrim, "\x00")
        fmt.Print("after trim found a null terminator at -- ")
        loc := bytes.Index(byteRespTrim, []byte("\x00"))
        fmt.Print(loc)
    

    打电话的时候我得到下面

    (master)⚡ % ./windows-dns-test create -d domain.com -n openshift-node-0 -t A -v 10.90.61.30                       
    found a null terminator at -- 2102
    total length = 2615
    after trim found a null terminator at -- 2102
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   eugenioy    6 年前

    从你的日志来看,冒犯的角色似乎在 2102 ,而整个数组 2615 元素。

    所以看起来 Trim 不会解决它,因为问题不一定是数组的最终字符。

    您是否尝试删除所有事件,使用 Replace 例如?

    byteRespTrim = bytes.Replace(byteRespTrim, []byte("\x00"), []byte{}, -1)