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

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().

图片转代码服务由CSDN问答提供 功能建议

如何将变量值连接到字节键值中?

  type结果struct {
 SummaryID int`json:“ summaryid”`
描述字符串`json:“ description”`
} 
 
byt:= [] byte(`
 {
“ fields”:  {
“项目”:
 {
“关键字”:“ DC” 
},
“摘要”:“测试” + Result.SummaryID,
“描述”:Result.Description,
“  issuetype“:{
” name“:” Bug“ 
} 
} 
}`)
   
 
 

注意: Result的值。 SummaryID Re sult.Description db.Query() 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)

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题