Go のインストールは成功しましたが、単純な Hello World が出力されません
php小编小新在Go语言学习过程中,有时可能会遇到安装成功但没有输出简单的Hello World的情况。这种情况可能是由于编码问题、环境配置错误或代码书写不正确等原因导致的。本文将为大家解决这一问题,提供一些常见的解决方法和技巧,以帮助大家顺利输出Hello World并顺利进行Go语言学习。
问题内容
使用 windows 11 pro,使用安装程序安装 go。 这是我的 main.go 文件:
package main import ( "fmt" "time" ) func main() { fmt.println("hello world") time.sleep(2 * time.second) }
以及我能想到的所有相关信息:
powershell版本:
ps c:\users\fares.alghazy\go\src> $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:\users\myuser\go\src> pwd c:\users\myuser\go\src ps c:\users\myuser\go\src> ls directory: c:\users\myuser\go\src mode lastwritetime length name ---- ------------- ------ ---- -a---- 28-jul-23 12:00 pm 122 main.go ps c:\users\myuser\go\src> go version go version go1.20.6 windows/amd64 ps c:\users\myuser\go\src> go env set go111module= set goarch=amd64 set gobin= set gocache=c:\users\myuser\appdata\local\go-build set goenv=c:\users\myuser\appdata\roaming\go\env set goexe=.exe set goexperiment= set goflags= set gohostarch=amd64 set gohostos=windows set goinsecure= set gomodcache=c:\users\myuser\go\pkg\mod set gonoproxy= set gonosumdb= set goos=windows set gopath=c:\users\myuser\go set goprivate= set goproxy=https://proxy.golang.org,direct set goroot=c:\program files\go set gosumdb=sum.golang.org set gotmpdir= set gotooldir=c:\program files\go\pkg\tool\windows_amd64 set govcs= set goversion=go1.20.6 set gccgo=gccgo set goamd64=v1 set ar=ar set cc=gcc set cxx=g++ set cgo_enabled=0 set gomod=nul set gowork= set cgo_cflags=-o2 -g set cgo_cppflags= set cgo_cxxflags=-o2 -g set cgo_fflags=-o2 -g set cgo_ldflags=-o2 -g set pkg_config=pkg-config set gogccflags=-m64 -fno-caret-diagnostics -qunused-arguments -wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=c:\users\myuser~1.alg\appdata\local\temp\go-build1704880455=/tmp/go-build -gno-record-gcc-switches
问题:
动作运行:
go 运行 main.go
预期输出:
> 你好世界
收到的输出:
终端停止(即使没有 time.sleep)然后什么也没有发生
尝试修复:
1-尝试使用 go build 然后运行 exe,没有收到输出
2-尝试构建然后将输出重定向到文件:
go 构建 main.go
.\main.exe >output.txt
output.txt 已创建但为空
3-检查控制台编码:活动代码页:65001
4-禁用防病毒或安全软件:我使用标准 windows 安全功能,未安装第 3 方安全软件 我什至尝试过重新安装go软件、重启电脑、重启vs code等
为什么我的代码没有显示输出?我可以做什么来修复它?
更新1: 按照@mkopriva 的指示:
go mod init my_app 生成以下 go.mod 文件:
module my_app go 1.20
我的新目录是:
PS C:\Users\myuser\Desktop\Development\go\my_app> ls Directory: C:\Users\myuser\Desktop\Development\go\my_app Mode 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 が出力されませんの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











redisstreamを使用してGo言語でメッセージキューを実装する問題は、GO言語とRedisを使用することです...

Go Crawler Collyのキュースレッドの問題は、Go言語でColly Crawler Libraryを使用する問題を調査します。 �...

Golandのカスタム構造ラベルが表示されない場合はどうすればよいですか?ゴーランドを使用するためにGolandを使用する場合、多くの開発者はカスタム構造タグに遭遇します...

Go言語での文字列印刷の違い:printlnとstring()関数を使用する効果の違いはGOにあります...

GO言語で構造を定義する2つの方法:VARとタイプのキーワードの違い。構造を定義するとき、GO言語はしばしば2つの異なる執筆方法を見ます:最初...

大企業または有名なオープンソースプロジェクトによって開発されたGOのどのライブラリが開発されていますか? GOでプログラミングするとき、開発者はしばしばいくつかの一般的なニーズに遭遇します...

SQL.Openを使用する場合、DSNがエラーを報告しないのはなぜですか? GO言語では、sql.open ...
