dsdtumf776629385 2018-07-16 07:41
浏览 394
已采纳

golang从指针类型切片复制指针值

type Order struct {
    *Res
    Status int
}

type Res struct {
    ResID            int64
    OtaBookID        string
    StayDetail       []*ResElement
    TotalCharge      float64
    CustFName        string
    CustLName        string
    CreateTime       time.Time
}

type ResElement struct {
    Res        *Res
    OtaEleID   string
    OtaRoomID  string
    RoomID     int
    Arrival    time.Time
    Depart     time.Time
    Charge     float64
    CreateTime time.Time
}

I have a slice named orderList to record orders. Now i have the first order which OtaBookID is "A123", and i want to copy this order and change its OtaBookID to "B123". The ResElement type have some other detail info to record this order and i hide them because it not affect my question. I can success change Res.OtaBookID but i have no idea how to change Res.StayDetail[0].Res.OtaBookID

func main() {
    var orderList []*Order

    res := new(Res)
    res.OtaBookID = "A123"

    resElt := new(ResElement)
    resElt.Res = res
    res.StayDetail = append(res.StayDetail, resElt)

    order := new(Order)
    order.Res = res

    orderList = append(orderList, order)

    originalOrder := new(Order)
    originalOrder.Res = new(Res)
    *originalOrder.Res = *order.Res
    //originalOrder.Res.StayDetail[0].Res.OtaBookID = "B123" //this will make all become "B123"

    originalOrder.Res.OtaBookID = "B123"

    orderList = append(orderList, originalOrder)

    fmt.Println(orderList[0].Res.OtaBookID)  //A123
    fmt.Println(orderList[1].Res.OtaBookID)  //B123
    fmt.Println(orderList[0].Res.StayDetail[0].Res.OtaBookID)  //A123
    fmt.Println(orderList[1].Res.StayDetail[0].Res.OtaBookID)  //A123, i want this become B123
}

The result i want is orderList[0].Res.OtaBookID and orderList[0].Res.StayDetail[0].Res.OtaBookID is "A123" and other is "B123"

ps: Because i'm taking over other's works. So the three type already fixed and can not adjust. I want to know in this situation whether have any method to arrive my goal

  • 写回答

1条回答 默认 最新

  • douji1077 2018-07-16 21:30
    关注

    From your example and clarification we can focus on main to achieve your goal:

    type Order struct {
        *Res
        Status int
    }
    
    type Res struct {
        ResID            int64
        OtaBookID        string
        StayDetail       []*ResElement
        TotalCharge      float64
        CustFName        string
        CustLName        string
        CreateTime       time.Time
    }
    
    type ResElement struct {
        Res        *Res
        OtaEleID   string
        OtaRoomID  string
        RoomID     int
        Arrival    time.Time
        Depart     time.Time
        Charge     float64
        CreateTime time.Time
    }
    
    func main() {
        var orderList []*Order
    
        res := new(Res)
        res.OtaBookID = "A123"
    
        resElt := new(ResElement)
        resElt.Res = res
        res.StayDetail = append(res.StayDetail, resElt)
    
        order := new(Order)
        order.Res = res
    
        orderList = append(orderList, order)
    
        originalOrder := new(Order)
        originalOrder.Res = new(Res)
    
        // The following statement will copy the values in the memory space of order to the memory
        // space of originalOrder, but take note that a pointer contains a memory address. Thus,
        // following this statement originalOrder.Res.StayDetail will point to the same address
        // as order.Res.StayDetail as slices are pointers...
        *originalOrder.Res = *order.Res
    
        // We now have to manually copy the slice to ensure it is different from the one in order.
        // We must create a new slice to avoid overwriting the source.
        originalOrder.Res.StayDetail := make([]*ResElement, len(order.Res.StayDetail))
    
        // Further, this slice contains pointers so we must copy these as well to ensure they do not
        // point to the ResElements of order.
        for i, v := range order.Res.StayDetail {
            re := new(ResElement)
    
            // copy the values of the memory in v to the values in the memory of re
            *re = *v
    
            // set re.Res to point to originalOrder.Res as it currently point to order.Res
            re.Res = originalOrder.Res
    
            // Now we must place re in the slice of originalOrder as it currently points to an
            // empty array of the correct size. Thus, we will not use append (which will grow
            // the size), we will just set the index to the correct value. Thus:
            originalOrder.StayDetail[i] = re
        }
        // The pointers and structures have now been properly set up, thus the following are equivalent:
        // originalOrder.OtaBookID = "B123" - Res via embedding
        // originalOrder.Res.OtaBookID = "B123" - Res directly
        // originalOrder.Res.StayDetail[0].Res.OtaBookID = "B123" - Res via ResElement pointer Res
        // They all point to the exact same memory space
    
        originalOrder.Res.OtaBookID = "B123"
    
        orderList = append(orderList, originalOrder)
    
        fmt.Println(orderList[0].Res.OtaBookID)  //A123
        fmt.Println(orderList[1].Res.OtaBookID)  //B123
        fmt.Println(orderList[0].Res.StayDetail[0].Res.OtaBookID)  //A123
        fmt.Println(orderList[1].Res.StayDetail[0].Res.OtaBookID)  //B123
    }
    

    Always remember that a pointer is just a memory address. If the address value is 0xffab67e1 a copy of the pointer will also contain the same value of 0xffab67e1 and effectively point to the same thing. Thus you must deliberately assign new memory space at a new address and copy the internal values. This must be done for any structures that contain pointers. We call this a Deep Copy or a Clone.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle