why this code breaks when i remove log?

agolangf · · 611 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>i made a code to get the system arch and it works!, but when i remove the log.Println AND the log import it breaks... lol (i&#39;m on windows x64, go x86)</p> <p><a href="http://i.imgur.com/ankgPOA.png" rel="nofollow">http://i.imgur.com/ankgPOA.png</a></p> <pre><code>package utils import ( &#34;log&#34; &#34;syscall&#34; &#34;unsafe&#34; ) var ( kernel32, _ = syscall.LoadLibrary(&#34;kernel32.dll&#34;) getNativeSystemInfo, _ = syscall.GetProcAddress(kernel32, &#34;GetNativeSystemInfo&#34;) ) func GetArch() (string, error) { // remove this line and &#34;log&#34; from import will break the code log.Println(&#34;&#34;) var lpSystemInfo struct { dwOemId struct { wProcessorArchitecture uint32 } } _, _, err := syscall.Syscall(getNativeSystemInfo, 1, uintptr(unsafe.Pointer(&amp;lpSystemInfo)), 0, 0) if err != 0 { return &#34;&#34;, err } switch lpSystemInfo.dwOemId.wProcessorArchitecture { case 9: return &#34;amd64&#34;, nil case 0: return &#34;386&#34;, nil } return &#34;&#34;, nil } </code></pre> <hr/>**评论:**<br/><br/>neonsteven: <pre><p>Your struct is missing a ton of fields. It is dangerous to use unsafe.Pointer to a struct that is the wrong size. GetNativeSystemInfo will overwrite part of your stack.</p> <p>You need to add all of these, with the right sizes and layouts:</p> <p><a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx</a></p></pre>Sk1LLb0X: <pre><p>that fixed it, thanks! but it&#39;s still weird that log.Println hides the error</p></pre>TheMerovius: <pre><p>No, that&#39;s pretty much normal. If you ever coded in an unsafe language (like C or C++) you&#39;ll have seen plenty of such mysterious errors. unsafe is called unsafe for a reason :) Memory corruptions lead to undefined behavior. Minimal changes in a program can lead to large differences in the layout of the memory space of a program. The combination of both means that you have &#34;spooky errors at a distance&#34;, programs that crash based on changing completely unrelated lines of code or the machine you are running your code on or the phase of the moon.</p></pre>daveddev: <pre><p>Without seeing more... There may be a race condition that Println() is hiding.</p></pre>travisby: <pre><p>define &#34;breaks&#34; -- what results do you get?</p></pre>Sk1LLb0X: <pre><p><a href="https://i.imgur.com/ankgPOA.png" rel="nofollow">https://i.imgur.com/ankgPOA.png</a> it compiles but fails while running</p></pre>peterbourgon: <pre><p>You know about <code>runtime.GOARCH</code>, right?</p></pre>Sk1LLb0X: <pre><p>thats for go&#39;s arch, not the system, if you use go x86 on win x64 it&#39;ll say you&#39;re on x86</p></pre>: <pre><p>[deleted]</p></pre>Sk1LLb0X: <pre><p>i did</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

611 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传