Skip to content

How to assign two slices containing different types? #38

Answered by switchupcb
listepo asked this question in Q&A
Discussion options

You must be logged in to vote

You want to generate a function that converts all from objects []*models.Currency to []*contracts.Currency.

  • []*models.Currency is a slice of struct pointers to a models.Currency struct.
  • []*contracts.Currency is a slice of struct pointers to a contracts.Currency struct.
  • These types are not equivalent and won't be assigned to each other by default.

There are multiple interpretations of the request to assign a slice of one type to a slice of another type.

  1. For each item in each slice, assign the respective fields from contracts.Currency to models.Currency.
  2. For each item in each slice, assign the respective interface contracts.Currency to models.Currency object (using type conversion).
  3. For …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@listepo
Comment options

@switchupcb
Comment options

Answer selected by listepo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants