dtyyrt4545 2019-03-03 02:14
浏览 225
已采纳

在Golang中替换数组结构中的数据

I have 3 struct data (GOLANG) that I call A, B, and C, struct C is result array replace between struct A and B when data is similar or more than 0 then I set all result to struct C using array.

Struct A, B, C {
 TransactionDate  string 
 TotalAmount      string 
 TotalTransaction string 
}

A = [
     {2019-02-01 0 0} 
     {2019-02-02 0 0} 
     {2019-02-03 0 0} 
     {2019-02-04 0 0} 
     {2019-02-05 0 0} 
     {2019-02-06 0 0} 
     {2019-02-07 0 0}
   ]

B = [
     {2019-02-02 1000 2} 
     {2019-02-07 200 3}
    ]

I expect the result is like

C = [
     {2019-02-01 0 0} 
     {2019-02-02 1000 2} 
     {2019-02-03 0 0} 
     {2019-02-04 0 0} 
     {2019-02-05 0 0} 
     {2019-02-06 0 0} 
     {2019-02-07 200 3}
  ]

I try using like this but I can't still like my expected result, can you help me to solve this?

func compareReplace() []C{
 var a []A
 var b []B
 var c []C   
 for i := 0; i < len(a); i++ { 
  if a[i].TransactionDate == b[i].TransactionDate {
        if b[i].TotalTransaction != "0" {
            c = append(c, b[i])
        }
  }
 }

 return c
}

Or we can collaborate at https://play.golang.org/p/H-aaolvSDZt

  • 写回答

2条回答 默认 最新

  • douxingti9307 2019-03-03 22:28
    关注

    Your logic assumes that the length of a is always the proper count to conditionally iterate through. The playground that @FatchulAmin shared in a comment on @EdChan answer exposed the issue when a is larger than b or vice versa, you will get the "index out of range" error because the smaller slice will no longer have indexes to match the larger. a[i].TransactionDate == b[i].TransactionDate

    For sanity, in this case, you should do a check to find the smallest count to iterate with, however, this will not allow you to completely check all of the largest slice.

    I suggest merging the slices then finding the largest and smallest to range and loop to remove what you want from the merged. NOTE: @EdChan is right to use one struct since they are all the same.

    type FooBar struct {
        TransactionDate  string
        TotalAmount      string
        TotalTransaction string
    }
    
    type FooBarSlice []FooBar // this is used as a receiver on func Remove
    
    func compareReplace(a []FooBar, b []FooBar) []FooBar {
        var c FooBarSlice
        c = append(a, b...)
    
    
        var largerSlice []FooBar
        var smallerSlice []FooBar
        if len(a) <= len(b) {
            largerSlice = b
            smallerSlice = a
        } else {
            largerSlice = a
            smallerSlice = b
        }
    
        for _, v := range smallerSlice {
    
            for j := 0; j < len(largerSlice); j++ {
                if largerSlice[j].TransactionDate == v.TransactionDate && largerSlice[j].TotalTransaction == "0" {
    
                    c.Remove(j)
    
                }
            }
        }
    
        return c
    }
    

    Full working example: https://play.golang.org/p/iyUYtXlDG54

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊