String Interpolation / Templating Variables in Go lang

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

String Interpolation / Templating Variables in Go lang

问题

假设我有以下字节字符串,表示一些 JSON 数据包:

  1. data := []byte(`{
  2. "Id" : 1,
  3. "Name": "John Doe",
  4. "Occupation": "gardener"
  5. }`)

我想知道,是否有一种动态创建这个 JSON 数据包的方法,例如:

  1. var NAME = "John Doe"
  2. var OCCUPATION = "gardener"
  3. data := []byte(`{
  4. "Id" : 1,
  5. "Name": {{ NAME }},
  6. "Occupation": {{ OCCUPATION }}
  7. }`)
英文:

Let's say I have the following byte string which represents some JSON packet:

  1. data := []byte(`{
  2. "Id" : 1,
  3. "Name": "John Doe",
  4. "Occupation": "gardener"
  5. }`)

I'm wondering, is there a way to create this JSON packet dynamically, e.g.,

  1. var NAME = "John Doe"
  2. var OCCUPATION = "gardener"
  3. data := []byte(`{
  4. "Id" : 1,
  5. "Name": {{ NAME }},
  6. "Occupation": {{ OCCUPATION }}
  7. }`)

答案1

得分: 3

你可以创建一个结构体并进行编组:

  1. type Data struct {
  2. ID int `json:"Id"`
  3. Name string `json:"Name"`
  4. Occupation string `json:"Occupation"`
  5. }
  6. data := Data{ID: 1, Name: "name", Occupation: "Occupation"}
  7. byteData, err := json.Marshal(data)

这样做也会在必要时处理数据中的转义字符。

另一种方法:

  1. data := map[string]interface{}{
  2. "Id": id,
  3. "Name": name,
  4. "Occupation": Occupation,
  5. }
  6. byteData, err := json.Marshal(data)

你也可以使用模板来实现,但是如果输入字符串包含需要转义的字符,有可能生成无效的 JSON 数据。

英文:

You can create a struct and marshal it:

  1. type Data struct {
  2. ID int `json:"Id"`
  3. Name string `json:"Name"`
  4. Occupation string `json:"Occupation"`
  5. }
  6. data:=Data{ ID:1, Name: "name", Occupation: "Occupation" }
  7. byteData, err:=json.Marshal(data)

This will also take care of escaping characters in data when necessary.

Another way:

  1. data:=map[string]interface{} {
  2. "Id": id,
  3. "Name": name,
  4. "Occupation": Occupation,
  5. }
  6. byteData, err:=json.Marshal(data)

You can also use templates for this, but then there is a chance you may generate invalid JSON data if the input strings contain characters that need to be escaped.

答案2

得分: 0

Go by Example网站上有一个关于使用encoding/json包的很好的页面:https://gobyexample.com/json

在你的特定情况下:

  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. )
  6. type Person struct {
  7. ID int `json:"Id"`
  8. Name string `json:"Name"`
  9. Occupation string `json:"Occupation"`
  10. }
  11. func main() {
  12. p := Person{ID: 1, Name: "John Doe", Occupation: "gardener"}
  13. log.Printf("Person data: %+v\n", p)
  14. d, err := json.Marshal(p)
  15. if err != nil {
  16. log.Fatalf("failed to marshal json: %v", err)
  17. }
  18. log.Printf("Encoded JSON: %s\n", d)
  19. }
  1. 2022/09/05 16:02:54 Person data: {ID:1 Name:John Doe Occupation:gardener}
  2. 2022/09/05 16:02:54 Encoded JSON: {"Id":1,"Name":"John Doe","Occupation":"gardener"}
英文:

Go by Example has a great page on using the encoding/json package: https://gobyexample.com/json

In your specific case:

  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. )
  6. type Person struct {
  7. ID int `json:"Id"`
  8. Name string `json:"Name"`
  9. Occupation string `json:"Occupation"`
  10. }
  11. func main() {
  12. p := Person{ID: 1, Name: "John Doe", Occupation: "gardener"}
  13. log.Printf("Person data: %+v\n", p)
  14. d, err := json.Marshal(p)
  15. if err != nil {
  16. log.Fatalf("failed to marshal json: %v", err)
  17. }
  18. log.Printf("Encoded JSON: %s\n", d)
  19. }
  1. 2022/09/05 16:02:54 Person data: {ID:1 Name:John Doe Occupation:gardener}
  2. 2022/09/05 16:02:54 Encoded JSON: {"Id":1,"Name":"John Doe","Occupation":"gardener"}

huangapple
  • 本文由 发表于 2022年9月5日 23:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/73611848.html
匿名

发表评论

匿名网友

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

确定