Your Question

I have a large array that needs to be inserted into mysql, so I wrote a function, passing the array as a parameter, and doing business processing in the function. However, in order to improve the versatility of the interface, I set the parameter to []interface{ }, the result is a memory out of bounds or null pointer exception. But if you don't use []interface{}, there will be no problem. Please tell me what I can do to improve the versatility of the function.

The document you expected this should be explained

Expected answer

Comment From: ivila

Can you provide a demo? Actually it's impossible for others to know what you are inferring to.

  1. what is the function you are using?
  2. what version of gorm you are using?
  3. what version of golang you are using?
  4. what is the "memory out of bounds or null pointer exception"

I suspect you are using Create method and passing an slice of interface, just like the following:

type Product struct {
        ID   uint64
        Name string
}
func demoFunc(db *gorm.DB, a []interface{}) error {
        return db.Create(&a).Error
}

func demoCaller(db *gorm.DB) {
        a := make([]interface{}, 2)
        a[0] = Product{Name: "product0"}
        a[1] = Product{Name: "product1"}
}

but what I got is: Gorm use create() batch insert have error

So can you provide a demo? Or we are just wasting time here.

Comment From: 13512263278

Can you provide a demo? Actually it's impossible for others to know what you are inferring to.

  1. what is the function you are using?
  2. what version of gorm you are using?
  3. what version of golang you are using?
  4. what is the "memory out of bounds or null pointer exception"

I suspect you are using Create method and passing an slice of interface, just like the following:

```go type Product struct { ID uint64 Name string } func demoFunc(db *gorm.DB, a []interface{}) error { return db.Create(&a).Error }

func demoCaller(db *gorm.DB) { a := make([]interface{}, 2) a[0] = Product{Name: "product0"} a[1] = Product{Name: "product1"} } ```

but what I got is: Gorm use create() batch insert have error

So can you provide a demo? Or we are just wasting time here. 1. I user create() 2. The gorm version I am using is v1.25.5 3. The golang version I am using is go1.20.5 windows/amd64

I define function following: `func batchInsert(batchSize int, arrStruct []interface{}, tableName string) error { totalStructs := len(arrStruct) numBatches := (totalStructs + batchSize - 1) / batchSize for i := 0; i < numBatches; i++ { start := i * batchSize end := (i + 1) * batchSize if end > totalStructs { end = totalStructs }

    batch := arrStruct[start:end]
    // 插入当前批次的结构体数组
    err := common.GORMICU.Table(tableName).Create(batch).Error
    if err != nil {
        return err
    }
    log.Printf("%s表第 %d 批次插入成功!\n", tableName, i+1)
}
return nil

}`

I want to implement a universal batch insertion interface. Because we want to calculate the length of the slice, we must use [] inteface {}, but [] inteface {} is reported as an error when sending to the conference

Comment From: ivila

@13512263278 I think there is a CreateInBatch method in gorm, you could just use that method or just copy from it

Comment From: 13512263278

@13512263278 I think there is a CreateInBatch method in gorm, you could just use that method or just copy from it Thanks a lot, I've solved the problem of inserting super large batches in batches by reflection