<p><a href="http://play.golang.org/p/qWK7g1seZw">http://play.golang.org/p/qWK7g1seZw</a></p>
<p>prog.go:7: cannot use "test" (type string) as type string in assignment</p>
<hr/>**评论:**<br/><br/>geetarista: <pre><p><a href="https://gist.github.com/aras-p/6224951">Related</a></p></pre>dvirsky: <pre><p>I actually found </p>
<pre><code> #define TRUE 0
#define FALSE 1
</code></pre>
<p>in the code of a developer that had been fired a few days before that.</p></pre>CaptainHayashi: <pre><p>IDK, in some cases (like program return values) this is a correct reading. As long as it's consistent :3</p></pre>dvirsky: <pre><p>That code worked. It was a bit wonky and unpredictable, but it did sorta work.</p></pre>hidden-username: <pre><p>Just curious, was he an old c programmer?</p></pre>dvirsky: <pre><p>no, he was about 30 at the time. </p></pre>gherald: <pre><p>Some men just want to watch the world burn</p></pre>THUNDERGROOVE: <pre><p>This is evil.</p></pre>media_guru: <pre><p>var false = true</p>
<p>also works ;-)</p></pre>DeedleFake: <pre><p>+<a href="/u/CompileBot">/u/CompileBot</a> go --with-errors</p>
<pre><code>package main
var true, false = false, true
func main() {
fmt.Println((1 == 1) == false)
}
</code></pre></pre>media_guru: <pre><p>Ah, that fails. Can only reassign one, I guess.</p></pre>clearingitup: <pre><p>No, you can, go just doesn't allow the previous statement because the order of assignments changes what the operation does. This works:</p>
<pre><code>var true, false = 1 == 0, 1 == 1
</code></pre></pre>borring: <pre><p>Wait, then how does <code>stringutil.Reverse()</code> work in the go examples? <a href="http://golang.org/doc/code.html" rel="nofollow">http://golang.org/doc/code.html</a></p>
<p>Is it because yours is a declaration?</p></pre>clearingitup: <pre><p>You're right, of course. A closer look at the error shows that</p>
<pre><code>var true, false = false, true
</code></pre>
<p>has a typechecking loop error... fixing this to</p>
<pre><code>var true, false bool = false, true
</code></pre>
<p>gives an initialization loop error. This error can be trimmed down to</p>
<pre><code>var false bool = false
</code></pre>
<p>I guess this is because it's trying to use a variable <em>while</em> it's being initialized, but I don't care enough to search for a conclusive answer.</p></pre>printf_hello_world: <pre><p>Yep.</p>
<p>I somehow missed the type declaration for a solid 30 seconds. Until then, my sanity was deeply questionable.</p></pre>THUNDERGROOVE: <pre><p>Honestly, that should be a compile time error.</p></pre>printf_hello_world: <pre><p>Yeah, it probably shouldn't be possible to define things that clash with builtin. </p>
<p>Example:</p>
<p><a href="http://play.golang.org/p/I2C0kL45Dp">http://play.golang.org/p/I2C0kL45Dp</a></p>
<p>vs.</p>
<p><a href="http://play.golang.org/p/QJTjpKfVca">http://play.golang.org/p/QJTjpKfVca</a></p>
<p>Or perhaps it shouldn't be possible to locally override any definitions at all. That can be <em>sort of</em> handy with variable names occasionally, but doesn't seem like it's worth the confusion.</p></pre>f2u: <pre><p>On the other hand, the current rules allow the introduction of new pre-defined types such as <code>rune</code>.</p></pre>printf_hello_world: <pre><p>Hm, I never thought about it from the extensibility angle.</p>
<p>I mean, a minimalist language like Go doesn't seem like it needs many more builtin types. However, predicting the future is pretty tough, and you're absolutely right that this is necessary in order to add new builtin types.</p></pre>4ur3l13n: <pre><p>No it is not built-in <a href="https://golang.org/ref/spec#Keywords" rel="nofollow">https://golang.org/ref/spec#Keywords</a></p></pre>printf_hello_world: <pre><p>builtin != keywords</p>
<p><a href="http://golang.org/pkg/builtin/">http://golang.org/pkg/builtin/</a></p>
<p>edit: but to be fair, I was using them misleadingly interchangeably in previous comments</p></pre>4ur3l13n: <pre><p>Oh thanks +printf_hello_world I do not know about that!</p></pre>kurin: <pre><p>How often will this actually bite someone though?</p></pre>thomasfr: <pre><p>Tip: Learn to read/write Haskell or something similar, after that you will spot things like this instantly. </p></pre>Hard_NOP_Life: <pre><p>Reminds me of <a href="http://stackoverflow.com/questions/30262509/union-object-acts-like-a-structure">this</a> question on stack overflow </p></pre>bot_bot_bot: <pre><p>Can someone explain this error for me?
I haven't started programming Go yet.</p></pre>seaofconfusion: <pre><p>He has redefined the type with name string to be an int. So when he declares a string it is actually an int and hilarity ensues trying to assign to it.</p></pre>bot_bot_bot: <pre><p>That is indeed evil! </p></pre>earthboundkid: <pre><p>You can't do this on the playground, but put it in another file with an unassuming name, so no one notices it. </p></pre>: <pre><p>[deleted]</p></pre>dvirsky: <pre><p>that is really evil</p></pre>Winsaucerer: <pre><p>How did you do that?</p></pre>lureker: <pre><p>'type string int' is at the end of the file.. (scroll down)</p></pre>mwholt: <pre><p>Or in the middle of a _test.go file to really confuse CI builds.</p></pre>m3talsmith: <pre><p>works ;P <a href="http://play.golang.org/p/ItdRLe2cy4" rel="nofollow">http://play.golang.org/p/ItdRLe2cy4</a></p></pre>tv64738: <pre><pre><code>type __ *[]*__
</code></pre></pre>calebdoxsey: <pre><p>Also: <a href="https://play.golang.org/p/6PUJYbSWDF" rel="nofollow">https://play.golang.org/p/6PUJYbSWDF</a></p>
<pre><code>package main
import "fmt"
func main() {
if "x" == "x" {
fmt.Println("this doesn't happen")
} else {
fmt.Println("but this does")
}
}
</code></pre></pre>jdtobe: <pre><p>This would certainly qualify.</p>
<p>It reminds me of one of my old coworkers. He insisted that UTF-8 source files was a bad idea, because if programmers <strong>can</strong> do this, they <strong>will</strong> do it, on every project.</p></pre>michaelbironneau: <pre><p>He may be right, but even with plain ascii, an unclear font in your editor can still wreak havoc. Back in the day of Visual Basic 6.0, 1's and l's looked exactly the same in Visual Studio. Once spent a whole day looking for a bug caused by a counter variable initialized to "l" instead of "1". </p></pre>codebloodedben: <pre><p>String is a reference, right? So do we have to use Compare() like so: <a href="https://play.golang.org/p/Y3zk47dBub" rel="nofollow">https://play.golang.org/p/Y3zk47dBub</a></p>
<p>I guess not. Mind=Blown</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传