Go Unpacking Array As Arguments

前端 未结 4 1993
南旧
南旧 2021-01-30 01:13

So in Python and Ruby there is the splat operator (*) for unpacking an array as arguments. In Javascript there is the .apply() function. Is there a way of unpacking an array/sli

4条回答
  •  梦毁少年i
    2021-01-30 01:46

    https://play.golang.org/p/2nN6kjHXIsd

    I had a reason to unpack some vars from a map[string]string with single quotes around some of them as well as without. Here's the logic for it and the play link up top has the full working snippet.

    func unpack(a map[string]string) string {
    
    var stmt, val string
    var x, y []string
    for k, v := range a {
        x = append(x, k)
        y = append(y, "'"+v+"'")
    }
    
    stmt = "INSERT INTO tdo.rca_trans_status (" + strings.Join(x, ", ")
    val = ") VALUES (" + strings.Join(y, ",") + ");"
    
    return stmt + val}
    

    Which presents cleanly for a mssql query as:

    INSERT INTO tdo.rca_trans_status (rca_json_body, original_org, md5sum, updated, rca_key) VALUES ('blob','EG','2343453463','2009-11-10 23:00:00','prb-180');
    

提交回复
热议问题