Go Can';是否从字节数组中删除空终止符?
我正在使用in-Go(在OSX上)与windows DNS服务器进行交互 当运行下面的代码段时,我得到一个关于空终止符的错误Go Can';是否从字节数组中删除空终止符?,go,Go,我正在使用in-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: Unma
$ ~/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
我怀疑当helper方法调用sprintf
将json响应转换为数组时,会添加一个空终止符
但是,即使在添加了一个字节.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
从您的日志中,似乎可以在位置
2102
找到有问题的字符,而整个数组有2615
元素
因此,看起来Trim
无法解决此问题,因为问题不一定是数组的最终字符
您是否尝试过删除所有引用,例如使用Replace
byteRespTrim = bytes.Replace(byteRespTrim, []byte("\x00"), []byte{}, -1)