0

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

1 Answer 1

1

A couple of points here that I'll break up, which mostly comes from lack of clarity in your question:

First

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.

Second

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, ","))

Third

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This data model is way overnormalized and sacifices hard to acquire performance (JOINs are more expensive than just reading a field) for relatively cheap disk space. Given the prices for storage today, that is a bad tradeoff.
@MarkusWMahlberg I actually disagree with that statement entirely. Disk is cheap these days, and so is computing. Yes this over complicates a simple data structure, but I wouldn't imagine he would only be building an application that only contains these features. Starting with good practices ultimately allows you to build a better application. "What tags does everyone use" is infinitely harder to answer in the previous approach.
There is a difference between computing and performance. You can scale computing as much as you like - if a query is more expensive than another one, it is going to take longer. And without proper use cases which justify it, there is no need to complicate a data model.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.