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条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏