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 有没有可以帮我搞一个微信建群链接,包括群名称和群资料群头像那种,不会让你白忙
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题