Search code examples
restcsvgogo-gin

How To Implement Import data Functionality With Golang?


I have 2 methods, which I am using for GET and POST. The first one is:

 var Join map[string]int

func MapTheFields(c *gin.Context) {
 var data []string
 //Open the csv file
 csvFile, _ := os.Open("customers.csv")
 reader := csv.NewReader(csvFile)
 line, _ := reader.ReadAll()
 for i := 0; i < len(line[0]); i++ {
     Join = map[string]int{
         line[0][i]: i,
     }
     data = append(data, line[0][i])
 }
 GetSuccessResponse(c, "The Mappings are:", data)

 }

Second one is also similar to the first. It just saves the values to the Database.

The problem I have been facing, is that I have to map the fields that I get from the csv file to the fields in my project, to do that I have made a map named Join as shown above and I am accessing the value of line in the second Function as

line[i][Join["Last Name"]]

But I am getting the value of Join["Last Name"] as 0 even though It's value is 1, and wherever I am using the join as an index the value is zero and I always end up with only the first 4 values and then an index out of bounds error.

Rest Code Is:

func ImportCustomerData(c *gin.Context) {
//Open the csv file
csvFile, _ := os.Open("customers.csv")
reader := csv.NewReader(csvFile)
var (
    user      models.User
    customer  models.Customer
    address   models.UserAddress
    addresses []models.UserAddress
    people    []models.Customer
    users     []models.User
)

line, _ := reader.ReadAll()
for i := 1; i < len(line[0]); i++ {

    //Initialize address details

    address.Address = line[i][Join["address"]]
    address.City = line[i][Join["City"]]
    address.State = line[i][Join["State"]]
    address.Zipcode = line[i][Join["Postal Code"]]

    savedAddress := SaveNewAddress(address, merchantDb)

    //Initalize user details
    user.FirstName = line[i][Join["First Name"]]
    user.LastName = line[i][Join["Last Name"]]
    user.CompanyName = line[i][Join["Company Name"]]
    user.EmailId = line[i][Join["Email"]]
    user.PhoneNumber = line[i][Join["Phone"]]
  }
}

The OutPut Of The First Function in Postman Is: { "response": { "code": 200, "api_status": 1, "message": "The Mappings are:", "data": [ "First Name", "Last Name", "Company Name", "Email", "Address", "City", "State", "Postal Code", "Phone", "Date Created", "Stripe ID", "Date of First Booking", "Date of Last Booking", "Frequency of Last Booking", "Date of Next Booking", "Notes", "Customer ID" ] } }

Where have I made a mistake?


Solution

  • You are assigning a new map each and every-time in MapTheFields():

     for i := 0; i < len(line[0]); i++ {
         Join = map[string]int{
             line[0][i]: i,
         }
         data = append(data, line[0][i])
     }
    

    Join map of type, should be allocated first, declare Join like this:

    Join = make(map[string]int) //declaration can be global
    

    Replace code snippet in MapTheFields() with this one:

     for i := 0; i < len(line[0]); i++ {
         Join[line[0][i]] = i
         data = append(data, line[0][i])
     }