<p>So as a programmer first getting their feet wet with Golang, I'm writing a simple command line blackjack program. When I was working on initializing an array of players, I was trying to set the balance of each player to start as 100 by doing the following
package main
import(
"fmt"
"strconv"
dealer "github.com/rcoverick/gocasino/dealer"
)</p>
<pre><code>func main(){
//p is read from command line
/*loading players */
players := make([]Player ,p+1)
for _,player := range players{
player.balance = 100
}
for i,player := range players{
fmt.Println("Player ",i," balance: $",player.balance)
}
}
</code></pre>
<p>however, the output was as though the balance didn't get set at all for the player structs. </p>
<pre><code>Number of players: 3
Player 0 balance: $ 0
Player 1 balance: $ 0
Player 2 balance: $ 0
</code></pre>
<p>However, when I change the range loop to be </p>
<pre><code>for i,_ := range players{
players[i].balance = 100
}
</code></pre>
<p>I get the expected result of </p>
<pre><code>Number of players: 3
Player 0 balance: $ 100
Player 1 balance: $ 100
Player 2 balance: $ 100
</code></pre>
<p>Is there something I'm missing here about the range function when performed on a slice? I thought the behavior was such that the second argument returned for each iteration of the for loop was linked to the underlying structure? or are certain scenarios of using range only return a copy of the value from the underlying container?</p>
<hr/>**评论:**<br/><br/>drvd: <pre><p>In </p>
<pre><code>for _,player := range players
</code></pre>
<p>The variable player is not a "reference" (as Go has no concept of reference) to the individual elements of the slice players, but a new variable. Its value is a <em>copy</em> of each slice element during iteration.</p>
<p>Take the Tour of Go <a href="https://tour.golang.org/" rel="nofollow">https://tour.golang.org/</a> especially Slides <a href="https://tour.golang.org/flowcontrol/1" rel="nofollow">https://tour.golang.org/flowcontrol/1</a> ff.</p></pre>codemonkey14: <pre><p>I had briefly gone through the Tour of Go but it just seemed a little weird coming from being used to foreach type of looping structures in other languages. Just wanted to make sure I was understanding it correctly. thanks for the advice!</p></pre>kostix: <pre><p>You should make yourself accustomed with the concept that in Go, just like in C, everything is passed by value (copied).
This includes variable assignments -- including your case with iteration with range, which "yields" a pair of values on each iteration, -- and passing arguments to functions.</p>
<p>When you need, you can use a pointer to explicitly pass "a reference" to a value around rather than the value itself.
In this case a pointer value is passed around using the same rules.</p>
<p>If you need to modify a slice's array's item when iterating using range, ignore the second value returned and use the indexing operator to access the slice.</p>
<p>TL;DR
You appear to not have too much experience with reasonably low-level programming languages, so you might get tripped again by some presumption you take for granted having had worked with more high-level PLs.
So I'd advise you to at least complete the tour.
The "Effective Go" document is also recommended.</p></pre>twisted1919: <pre><p>Here's an alternative that will do what you are after, using pointers: <a href="https://play.golang.org/p/C5HXyP-m_I" rel="nofollow">https://play.golang.org/p/C5HXyP-m_I</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传