Cant UnMarshel array of values and headers to struct

I am trying to parse users from csv to its struct one by one. But I am not sure whats the best way or method to achieve this. Please have a look at below code. Any help is really appreciated

recordFile, err := os.Open(csv_file_path)
if err != nil {
	return "Couldnt open file"

reader := csv.NewReader(recordFile)
headers, err := reader.Read()
if err != nil {
	return "Couldnt get headers"

fmt.Printf("Headers : %v \n", headers)
var user user_schema.User

for i := 0; ; i = i + 1 {
	record, err := reader.Read()
	if err == io.EOF {
		break // reached end of the file
	} else if err != nil {
		return "Error in retrieving record"

	eerr := json.Unmarshal(record, &user) // <- Problem is here, how do I convert headers and array of values to User struct
	if eerr != nil {

Read returns a []string. You will have to iterate over this for the header line to get the column names. Then for each row, iterate of the []string for this row.

BTW: What is user_schema.User?

1 Like
type User struct {
 ID           uint `gorm:"primary_key"`
 UserName     string `validate:"required"`
 Email        string `validate:"required,email"`
 PwdHash     string `validate:"required" json:"pwd_hash"`
 Password     string `gorm:"-"`
 PhoneNumber  string
 IsActive     int8
 LastActive   string
 CreatedAt    time.Time
 UpdatedAt    time.Time

I am trying to seed db from a csv file(will make it dynamic so passing any table schema and its csv file path would seed data anytime I want)