what syntax for add data Array into database, i found for postgresql is : pg.Array
// "ins" is the SQL insert statement
ins := "INSERT INTO posts (title, tags) VALUES ($1, $2)"
// "tags" is the list of tags, as a string slice
tags := []string{"go", "goroutines", "queues"}
// the pq.Array function is the secret sauce
_, err = db.Exec(ins, "Job Queues in Go", pq.Array(tags))
A couple of points here that I'll break up, which mostly comes from lack of clarity in your question:
pq.Array
is used to convert array values into safe lists in PostgreSQL, such as the following statement:
db.Query(`SELECT * FROM t WHERE id = ANY($1)`, pq.Array([]int{235, 401}))
Where the resulting query is:
SELECT * FROM t WHERE id = ANY(235, 401)
This is intended to help you safely craft query values irrelevant of types, from lists, which is not how you're using it with your question.
If you're simply trying to marshal the value into a comma-separated list in your database column, ie:
| title | tags |
|---------|----------------------|
| my post | go,goroutines,queues |
You don't need a special function within the SQL driver. You simply need to create the value, and let the prepared statement do its thing:
tags := []string{"go, goroutines, queues"}
q := "INSERT INTO posts (title, tags) VALUES ($1, $2)"
_, _ = db.Exec(q, "mypost", strings.Join(tags, ","))
You'd probably be even BETTER served, using relationships within MySQL to accomplish what you're doing:
posts| id | title |
|----|---------|
| 1 | my post |
tags| id | tag |
|----|------------|
| 1 | go |
| 2 | goroutines |
| 3 | queues |
posts_tags| posts_id | tags_id |
|----------|---------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
This will help you save space by not saving duplicate data, and remove the need to understand the serialization method within the database (plus, this is what relational databases do). Then, when you select the table, you can craft JOIN
statements to retrieve the data as needed. I highly recommend reading up on many to many relationships within MySQL.