SoFunction
Updated on 2025-03-05

Example code for Golang using CopyIn for batch creation

Preface

This article introduces two points:

  • How to use copyin in golang
  • How to insert special character jsonb (need to explicitly force conversion to string type)
package main
import (
    "fmt"
    "/jinzhu/gorm"
    "/lib/pq"
)
func main() {
    (1)
    dbConfig := ("host=localhost user=postgres dbname=postgres sslmode=disable password=123456 application_name=xxx")
    ("db:", dbConfig)
    engine, e := ("postgres", dbConfig)
    if e != nil {
        panic(e)
    }
    driverdb := ()
    tx,e := ()
    if e!=nil {
        panic(e)
    }
    // The table name is test    // The fields are age-integer, name-varchar, attach-jsonb    stmt, e := (("test", "age","name", "attach"))
    if e != nil {
        ()
        panic(e)
    }
    defer ()
    for i := 0; i < 10000; i++ {
       var attach = map[string]interface{}{
            "money": 19,
            "mother": "Fangfang",
        }
        buf, _ := (attach)
        _, e := (i, "Zhang San", string(buf))
        if e != nil {
            ()
            panic(e)
        }
    }
    // Execution must be empty once to indicate the end.  Otherwise it will block    ()
    ()
}

This is the article about the sample code that Golang uses CopyIn for batch creation. For more related content on batch creation of Golang CopyIn, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!