0

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?

7
  • post rest of the code, by removing unnecessary details which aren't required.. Commented Oct 5, 2018 at 11:21
  • Inside The For Loop of the rest code the index out of bound error is coming Commented Oct 5, 2018 at 11:30
  • Consider updating your first function code and then, specify what error you are getting now Commented Oct 5, 2018 at 11:49
  • i am not getting any output in postman Commented Oct 5, 2018 at 11:50
  • you are not creating new user on each iteration, and you are not appending it to users, check your logic. Commented Oct 5, 2018 at 11:57

1 Answer 1

1

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])
 }
Sign up to request clarification or add additional context in comments.

4 Comments

@HarshitVadhera you need to allocate memory, using make, create Join using make
But i Want to access the value of join in the second Function thats Why I have Declared Globaly Join See Over The First Function
@HarshitVadhera If that's the case, declare Join globally, but assign locally in some function Join = make(map[string]int), this will work
The Problem is Coming in the Second Function First is Fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.