<p>I've been googling, perhaps poorly, for any information regarding current or future implementation of final/val (Java/Kotlin) variable assignments. Does anyone know if it has been suggested for 2+ or at all? If so what was the response?</p>
<p>My apologies if this has been asked before.</p>
<hr/>**评论:**<br/><br/>knotdjb: <pre><p>Go has <code>const</code>, e.g. <code>const foo int = 5</code>. Is this what you want? The value of foo cannot be changed.</p></pre>HolyClickbaitBatman: <pre><p>Thank you, but no I know about that and that is not what I meant. What I mean is:</p>
<p>Java:</p>
<pre><code>public void Work() {
final int i = repo.GetInt();
i = repo.GetInt(); //error
}
</code></pre>
<p>What I imagined it should look like in go:</p>
<pre><code>func Work() error {
val i := repo.Int()
i = repo.Int() //error
}
</code></pre>
<p>So, single assignment variable as method result some other non-constant runtime value.</p></pre>HectorJ: <pre><p>So, a <code>const</code> but with a value computed at runtime, for local variables, and I'm guessing also parameters, struct fields, etc.</p>
<p>No such thing in Go AFAIK (sadly).</p></pre>mrichman: <pre><p>Is this the same as C#'s <code>readonly</code> modifier?</p></pre>HolyClickbaitBatman: <pre><p>Yes and no. C# has no final/val equivalent for function local variables. It does have readonly and sealed for members and methods/classes respectively.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传