Golang []byte to PHP string

huangapple go评论76阅读模式
英文:

Golang []byte to PHP string

问题

我目前正在使用Golang将[]byte数组写入文件(下面是代码)。在将其写入文件后,我想在PHP中打开文件并读取字符串。然而,由于它是编码的,我需要使用unpack函数?我不确定要使用哪种格式。

这是我的Golang代码:

var jsonlen uint16
// 16KB输出缓冲区
wbuf := bufio.NewWriterSize(os.Stdout, 16384)

// 写入魔术字节
fmt.Print(MagicBytes)

// 编码并写入json
json, err := json.Marshal(Metadata)
if err != nil {
    fmt.Println("Failed to encode the Metadata JSON:", err)
    return
}
jsonlen = uint16(len(json))

err = binary.Write(wbuf, binary.LittleEndian, &jsonlen)
if err != nil {
    fmt.Println("error writing output:", err)
    return
}

err = binary.Write(wbuf, binary.LittleEndian, &json)
if err != nil {
    fmt.Println("error writing output:", err)
    return
}

我还没有编写任何永久的PHP代码,所以这里没有可以展示的代码。

英文:

I'm currently writing a []byte array with Golang to a file (code below). After I have written it to the file, I want to open the file in PHP and read the string. However, since it's encoded I need to use unpack? I'm not sure what format(s) to use.

Here is my Golang code:

var jsonlen uint16
// 16KB output buffer
wbuf := bufio.NewWriterSize(os.Stdout, 16384)

// write the magic bytes
fmt.Print(MagicBytes)

// encode and write json
json, err := json.Marshal(Metadata)
if err != nil {
	fmt.Println("Failed to encode the Metadata JSON:", err)
	return
}
jsonlen = uint16(len(json))

err = binary.Write(wbuf, binary.LittleEndian, &jsonlen)
if err != nil {
	fmt.Println("error writing output: ", err)
	return
}

err = binary.Write(wbuf, binary.LittleEndian, &json)
if err != nil {
	fmt.Println("error writing output: ", err)
	return
}

Haven't written any permanent PHP code yet so don't have any to show here.

答案1

得分: 0

使用以下代码对其进行排序:

wbuf.Write(json)

请注意,这是原文的直译,可能需要根据上下文进行适当调整。

英文:

Sorted this by using:

wbuf.Write(json)

huangapple
  • 本文由 发表于 2016年3月4日 15:25:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/35790314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定