php小编小新在Go语言学习过程中,有时可能会遇到安装成功但没有输出简单的Hello World的情况。这种情况可能是由于编码问题、环境配置错误或代码书写不正确等原因导致的。本文将为大家解决这一问题,提供一些常见的解决方法和技巧,以帮助大家顺利输出Hello World并顺利进行Go语言学习。
问题内容
使用 windows 11 pro,使用安装程序安装 go。这是我的 main.go 文件:
package mainimport ( "fmt" "time")func main() { fmt.println("hello world") time.sleep(2 * time.second)}
登录后复制
以及我能想到的所有相关信息:powershell版本:ps c:usersares.alghazygosrc> $psversiontable.psversion
主要次要构建修订
5 1 22621 1778
go 扩展设置.json:
{ "explorer.confirmdelete": false, "terminal.integrated.scrollback": 100000, "editor.minimap.enabled": false, "[javascript]": { "editor.defaultformatter": "esbenp.prettier-vscode" }, "workbench.editorassociations": { "*.db": "default", "*.docx": "default" }, "[json]": { "editor.defaultformatter": "vscode.json-language-features" }, "[php]": { "editor.defaultformatter": "sophisticode.php-formatter" }, "editor.formatonsave": true, "prettier.tabwidth": 4, "[typescript]": { "editor.defaultformatter": "vscode.typescript-language-features" }, "[dockercompose]": { "editor.defaultformatter": "ms-azuretools.vscode-docker" }, "typescript.updateimportsonfilemove.enabled": "always", "javascript.updateimportsonfilemove.enabled": "always", "[jsonc]": { "editor.defaultformatter": "esbenp.prettier-vscode" }, "[html]": { "editor.defaultformatter": "esbenp.prettier-vscode" }, "[typescriptreact]": { "editor.defaultformatter": "vscode.typescript-language-features" }, "diffeditor.ignoretrimwhitespace": false, "workbench.startupeditor": "none", "[vue]": { "editor.defaultformatter": "vue.volar" }, "audiocues.linehasbreakpoint": "off", "audiocues.linehaserror": "off", "audiocues.linehasinlinesuggestion": "off", "audiocues.noinlayhints": "off", "audiocues.ondebugbreak": "off", "audiocues.volume": 0, "audiocues.linehasfoldedarea": "off", "go.gopath": "c:\users\myuser\go", "go.goroot": "c:\program files\go", "files.autosave": "afterdelay", "go.alternatetools": { }}
登录后复制
系统/目录信息:
ps c:usersmyusergosrc> pwdc:usersmyusergosrcps c:usersmyusergosrc> ls directory: c:usersmyusergosrcmode lastwritetime length name---- ------------- ------ -----a---- 28-jul-23 12:00 pm 122 main.gops c:usersmyusergosrc> go versiongo version go1.20.6 windows/amd64ps c:usersmyusergosrc> go envset go111module=set goarch=amd64set gobin=set gocache=c:usersmyuserppdatalocalgo-buildset goenv=c:usersmyuserppdataoaminggoenvset goexe=.exeset goexperiment=set goflags=set gohostarch=amd64set gohostos=windowsset goinsecure=set gomodcache=c:usersmyusergopkgmodset gonoproxy=set gonosumdb=set goos=windowsset gopath=c:usersmyusergoset goprivate=set goproxy=https://proxy.golang.org,directset goroot=c:program filesgoset gosumdb=sum.golang.orgset gotmpdir=set gotooldir=c:program filesgopkgoolwindows_amd64set govcs=set goversion=go1.20.6set gccgo=gccgoset goamd64=v1set ar=arset cc=gccset cxx=g++set cgo_enabled=0set gomod=nulset gowork=set cgo_cflags=-o2 -gset cgo_cppflags=set cgo_cxxflags=-o2 -gset cgo_fflags=-o2 -gset cgo_ldflags=-o2 -gset pkg_config=pkg-configset gogccflags=-m64 -fno-caret-diagnostics -qunused-arguments -wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=c:usersmyuser~1.algppdatalocalempgo-build1704880455=/tmp/go-build -gno-record-gcc-switches
登录后复制
问题:
动作运行:go 运行 main.go预期输出:> 你好世界收到的输出:终端停止(即使没有 time.sleep)然后什么也没有发生
尝试修复:1-尝试使用 go build 然后运行 exe,没有收到输出2-尝试构建然后将输出重定向到文件:go 构建 main.go.main.exe >output.txtoutput.txt 已创建但为空3-检查控制台编码:活动代码页:65001
4-禁用防病毒或安全软件:我使用标准 windows 安全功能,未安装第 3 方安全软件我什至尝试过重新安装go软件、重启电脑、重启vs code等
为什么我的代码没有显示输出?我可以做什么来修复它?
更新1:按照@mkopriva 的指示:
go mod init my_app 生成以下 go.mod 文件:
module my_appgo 1.20
登录后复制
我的新目录是:
PS C:UsersmyuserDesktopDevelopmentgomy_app> lsDirectory: C:UsersmyuserDesktopDevelopmentgomy_appMode LastWriteTime Length Name---- ------------- ------ -----a---- 28-Jul-23 1:14 PM 27 go.mod-a---- 28-Jul-23 1:15 PM 125 main.go
登录后复制
go run main.go 或构建然后运行没有任何变化。仍然没有得到输出。
解决方法
Go 程序经常被防病毒软件错误地识别为恶意软件,尽管它们并非恶意软件。 Go 官方文档中甚至还有有关此主题的常见问题解答条目。
Windows Defender 通常会在编译后立即删除您的可执行文件。您可以不使用 go run main.go ,而是分别执行两个步骤,首先调用 go build main.go ,然后尝试运行 main.exe 。当 Windows 在创建后立即删除可执行文件时,此操作将会失败。我曾见过编译的 DLL 在一分多钟后消失,当时我以为自己是安全的,因此请注意,可执行文件可能不会立即被删除,但可能会在编译后几秒甚至几分钟被删除。
当您调用 go run main.go 时,它将在 %APPDATA% 的 Local 路径下的临时文件夹中构建可执行文件,该文件夹名为 go-build 。
您可能希望在 Windows Defender 中创建例外规则,以忽略 APPDATA 中的 go-build 文件夹。您还应该从防病毒扫描中排除 GOPATH 和 GOBIN,您可能也会在那里构建和运行代码。
以上就是Go安装成功但没有输出简单的Hello World的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2354748.html