Persist JSON Postgres / Golang

Asked

Viewed 317 times

1

Hello, is there any way to persist a JSON in postgres with the GO language?

I don’t want to do a manual Insert, I want to map my JSON with a struct and persist, similar to Hibernate in Java.

found this lib http://godoc.org/gopkg.in/mgo.v2/bson

Convert my JSON to BSON and persist?

How would that look in the code?

2 answers

1

Hibernate is a ORM. For Go, it’s four most popular options:

I’ve never used them... And most likely, I never will. I, like many developers, have decided that Orms are not usually useful. I prefer to write my own SQL, because it’s not hard, it gives me better control, it often gives me better performance, and it means I always understand what my code is doing.

0

I use the GORM and is very easy to move.

After you give a read will understand, something like this, you can use BSON to generate your UUID string

type Modelo struct {
    UUID string `gorm:"primary_key"`
    Payload string `sql:"type:JSONB NOT NULL DEFAULT '{}'::JSONB"`
}

db.Save(&modelo)

For the payload field you make a json. Marshal();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.