Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I will answer your question pretty much like others have done. However, not that the problem you try to solve is not best served by looping over a range of values. Read on:</p> <h2>Solution to your question</h2> <p>Like others have said, <code>for-range</code> provide an immutable iteration over the range of values. That means any change you make to the value provided in the iteration will be lost. It's basically giving you a copy of the real value, not the actual value.</p> <pre><code>for _, item := range box.BoxItems { // ^-not the real `item`, it's a copy! </code></pre> <p>A way around this is to keep track of the indexing value in the <code>for idx, val := range</code>, and use this <code>idx</code> to address the value you look for directly.</p> <p>If you change your for-loop to keep the index value:</p> <pre><code>for i, item := range box.BoxItems { // ^-keep this </code></pre> <p>You will be able to reference the actual item in the array you loop on:</p> <pre><code>for i, item := range box.BoxItems { // Here, item is a copy of the value at box.BoxItems[i] if item.Id == boxItem.Id { // Refer directly to an item inside the slice box.BoxItems[i].Qty++ return box.BoxItems[i] // Need to return the actual one, not the copy } } </code></pre> <p><a href="http://play.golang.org/p/_ElsdshWiY" rel="nofollow">Playground</a></p> <p>I would favor this approach over the <code>for i; i&lt;Len; i++</code> one as I find it more readable. But this is simply a matter of taste and the <code>for i</code> form will be more efficient (beware of premature-optimization!).</p> <h2>Your real problem is</h2> <p>What you're trying to do is to avoid duplicating <code>BoxItems</code> if their <code>Id</code> already exists. To do this, you iterate over the whole range of the <code>box.BoxItems</code> slice. If you have <code>N</code> items in your <code>box.BoxItems</code> slice, you will potentially iterate over all <code>N</code> items before finding out that the item you're looking for doesn't exist! Basically, this means your algorithm is O(N).</p> <h3>If you increment <code>Id</code> in natural order</h3> <p>That is, <code>0, 1, 2, 3, ..., n - 1, n</code>, you can keep using a slice to index your box items. You would do like this:</p> <pre><code>func (box *Box) AddBoxItem(boxItem BoxItem) BoxItem { // Lookup your item by Id if boxItem.Id &lt; len(box.BoxItems) { // It exists, do don't create it, just increment item := box.BoxItems[boxItem.Id] item.Qty++ box.BoxItems[boxItem.Id] = item return item } // New item so append box.BoxItems = append(box.BoxItems, boxItem) return boxItem } </code></pre> <p><a href="http://play.golang.org/p/Cj6wMUqlQV" rel="nofollow">Playground</a></p> <h3>If you increment <code>Id</code> in any order</h3> <p>You should use a datastructure that offers fast lookups, such as the built-in <code>map</code>, which offers O(1) lookups (that means, you need to do a single operation to find your item, not <code>n</code> operations).</p> <pre><code>type Box struct { BoxItems map[int]BoxItem } func (box *Box) AddBoxItem(boxItem BoxItem) BoxItem { // Lookup the map by Id item, ok := box.BoxItems[boxItem.Id] if ok { // It exists, do don't create it, just increment item.Qty++ } else { item = boxItem } // New item so add it to the map box.BoxItems[boxItem.Id] = item return item } </code></pre> <p><a href="http://play.golang.org/p/nH0pkgUrMW" rel="nofollow">Playground</a></p> <p>This is a more correct way to solve your problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload