dongmu1996 2018-10-05 11:02
浏览 80
已采纳

如何使用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?

  • 写回答

1条回答 默认 最新

  • doutiaosu2310 2018-10-05 11:36
    关注

    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])
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘