Golang []byte to PHP string

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

Golang []byte to PHP string

问题

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

这是我的Golang代码:

  1. var jsonlen uint16
  2. // 16KB输出缓冲区
  3. wbuf := bufio.NewWriterSize(os.Stdout, 16384)
  4. // 写入魔术字节
  5. fmt.Print(MagicBytes)
  6. // 编码并写入json
  7. json, err := json.Marshal(Metadata)
  8. if err != nil {
  9. fmt.Println("Failed to encode the Metadata JSON:", err)
  10. return
  11. }
  12. jsonlen = uint16(len(json))
  13. err = binary.Write(wbuf, binary.LittleEndian, &jsonlen)
  14. if err != nil {
  15. fmt.Println("error writing output:", err)
  16. return
  17. }
  18. err = binary.Write(wbuf, binary.LittleEndian, &json)
  19. if err != nil {
  20. fmt.Println("error writing output:", err)
  21. return
  22. }

我还没有编写任何永久的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:

  1. var jsonlen uint16
  2. // 16KB output buffer
  3. wbuf := bufio.NewWriterSize(os.Stdout, 16384)
  4. // write the magic bytes
  5. fmt.Print(MagicBytes)
  6. // encode and write json
  7. json, err := json.Marshal(Metadata)
  8. if err != nil {
  9. fmt.Println("Failed to encode the Metadata JSON:", err)
  10. return
  11. }
  12. jsonlen = uint16(len(json))
  13. err = binary.Write(wbuf, binary.LittleEndian, &jsonlen)
  14. if err != nil {
  15. fmt.Println("error writing output: ", err)
  16. return
  17. }
  18. err = binary.Write(wbuf, binary.LittleEndian, &json)
  19. if err != nil {
  20. fmt.Println("error writing output: ", err)
  21. return
  22. }

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

答案1

得分: 0

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

  1. wbuf.Write(json)

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

英文:

Sorted this by using:

  1. 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:

确定