How do I use JSONB columns using database/sql with MySQL?

Using JSONB columns in MySQL can be quite effective for managing data that has varying structures. In Go, the database/sql package can be utilized to interact with MySQL databases that have JSONB fields. This allows developers to work with JSON data seamlessly.

Here's a simple example of how you can use JSONB columns in Go with MySQL:

package main import ( "database/sql" "encoding/json" "fmt" _ "github.com/go-sql-driver/mysql" ) type User struct { ID int `json:"id"` Data map[string]interface{} `json:"data"` } func main() { // Set up the database connection db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/your_database") if err != nil { panic(err) } defer db.Close() // Create a sample JSON object userData := map[string]interface{}{ "name": "John Doe", "age": 30, "address": "123 Main St, Anytown, USA", } // Marshal the JSON object jsonData, err := json.Marshal(userData) if err != nil { panic(err) } // Insert data into the database _, err = db.Exec("INSERT INTO users (data) VALUES (?)", jsonData) if err != nil { panic(err) } // Querying data from the database rows, err := db.Query("SELECT id, data FROM users") if err != nil { panic(err) } defer rows.Close() for rows.Next() { var user User var jsonData []byte if err := rows.Scan(&user.ID, &jsonData); err != nil { panic(err) } json.Unmarshal(jsonData, &user.Data) fmt.Printf("User ID: %d, Data: %+v\n", user.ID, user.Data) } }

JSONB MySQL Go database/sql JSON Golang