<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'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 (
"log"
"syscall"
"unsafe"
)
var (
kernel32, _ = syscall.LoadLibrary("kernel32.dll")
getNativeSystemInfo, _ = syscall.GetProcAddress(kernel32, "GetNativeSystemInfo")
)
func GetArch() (string, error) {
// remove this line and "log" from import will break the code
log.Println("")
var lpSystemInfo struct {
dwOemId struct {
wProcessorArchitecture uint32
}
}
_, _, err := syscall.Syscall(getNativeSystemInfo, 1, uintptr(unsafe.Pointer(&lpSystemInfo)), 0, 0)
if err != 0 {
return "", err
}
switch lpSystemInfo.dwOemId.wProcessorArchitecture {
case 9:
return "amd64", nil
case 0:
return "386", nil
}
return "", 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's still weird that log.Println hides the error</p></pre>TheMerovius: <pre><p>No, that's pretty much normal. If you ever coded in an unsafe language (like C or C++) you'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 "spooky errors at a distance", 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 "breaks" -- 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's arch, not the system,
if you use go x86 on win x64 it'll say you're on x86</p></pre>: <pre><p>[deleted]</p></pre>Sk1LLb0X: <pre><p>i did</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传