'Take overlapping properties of struct A and fillup same values in struct B

one of the anoying parts in go is everytime this damn manually conversion from struct type to struct type.

for examople i have this two struct types:

type BasketItem struct {
  ProductId int
  Quantity int
  Price float64
  Tax float64
  BasketId int
  UserId int
}

type BillingItem struct {
      ProductId int
      Quantity int
      Price float64
      Tax float64
      BillingId int
      UserId int
      CustomerGroup int
      CreationDate int
      OtherFieldsThatIDontWannaHaveInStructA string
    }

So now if i have a BasketItem with values then i wanna now create just a BillingItem with the same values prefiled like:

basketItem := BasketItem{ProductId:5,Quantity:5,Price:4.50,UserId:0, BasketId:10}
billingItem := BillingItem{basketItem}
print(billingItem.Price) // should be 4.50

I now i can make a structs that have all the overlapping properties and then extend the other two structs from that but honestly this is not a real world example and therefor is not a good solution for me. it interessets me how this generally work with two totally different structs from different modules but some overlaping (same named) properties.

For Example Gorm also in the Response just fill up the matched fields of a struct and skip the others...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source