N
N
nano_e_t_42017-09-08 16:27:57
go
nano_e_t_4, 2017-09-08 16:27:57

How to pass values ​​to a request?

All fight
I have, for example, such a map:
m = {"a": 1, "b": 2, "c": 3, "d": 4}
I need to unpack it into a string in this format:
inserto into table la ("a", "b", "c", "d") values ​​(1,2,3,4)
now I do it simply and stupidly, I run through the entire map in a loop and do +:

for key, value := range tags {
      temp_columns = temp_columns + "\"" + key + "\"" + ","
      temp_values = temp_values + "'" + value + "'" + ","
    }

but this is probably not the most beautiful and good method. If there is a better\more convenient\prettier way, please tell
me. The
essence of the task is as follows: 2 maps arrive in the daemon in the format map[string]string and map[string]interface{} (this cannot be changed on the other side). For this case, you need to create a table in Postgres and insert these values ​​into it. As I already wrote, now I’m doing everything stupidly, I sort through all the map values ​​​​and through fmt.Sprintf I form rows for create Table and insert. But I want to be smart (the 21st century, after all)
, yes, to work with the database I use the standard libraries "database / sql" and _ "github.com/lib/pq"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2017-09-08
@RidgeA

I don't see anything wrong with simple and understandable code.
but if you really want to - you can try something like
https://golang.org/pkg/text/template/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question