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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题