dtbonklcs575884485 2016-12-14 15:19
浏览 315
已采纳

golang如何将[] byte键值与其他变量连接

How to concatenate variable value into the byte key values ?

type Result struct {
        SummaryID       int              `json:"summaryid"`
        Description     string           `json:"description"` 
    }

byt := []byte(`
                        {
                            "fields": {                                                              
                               "project":
                               { 
                                  "key": "DC"
                               },
                               "summary": "Test" + Result.SummaryID,    
                               "description": Result.Description,
                               "issuetype": {
                                  "name": "Bug"
                               }
                           }
                        }`)

Note: values of Result.SummaryID and Result.Description return from the db.Query() and rows.Scan().

  • 写回答

1条回答 默认 最新

  • dongyupan4850 2016-12-15 03:09
    关注

    Go doesn't support string interpolation, so you'll have to use something like fmt.Sprintf or the template package if you want to compose strings out of smaller substrings.

    You can do the former like so:

    var buf bytes.Buffer
    byt := []byte(fmt.Sprintf(`
                        {
                            "fields": {
                              "project":
                               { 
                                  "key": "DC"
                               },
                               "summary": "Test%d",
                               "description": "%s",
                               "issuetype": {
                                  "name": "Bug"
                               }
                           }
                        }`, result.SummaryID, result.Description))
    

    Though I would really advise against it, since the encoding/json package is designed for safely and sanely outputting JSON strings.

    Here's an example that uses struct embedding for the main object, and maps elsewhere to demonstrate both approaches.

    type WrappedResult struct {
        Project map[string]string `json:"project"`
        Result
        IssueType map[string]string `json:"issuetype"`
    }
    
    byt, err := json.MarshalIndent(map[string]interface{}{
        "fields": WrappedResult{
            Result: result,
            Project: map[string]string{ "key": "DC" },
            IssueType: map[string]string{ "name": "Bug" },
         },
    });
    

    (note that your type declaration contradicts your JSON example in that the former specifies summaryid but the latter has summary)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计