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 サイトの他の関連記事を参照してください。

Golangは高い並行性タスクにより適していますが、Pythonには柔軟性がより多くの利点があります。 1.Golangは、GoroutineとChannelを介して並行性を効率的に処理します。 2。Pythonは、GILの影響を受けるが、複数の並行性メソッドを提供するスレッドとAsyncioに依存しています。選択は、特定のニーズに基づいている必要があります。

GolangとCのパフォーマンスの違いは、主にメモリ管理、コンピレーションの最適化、ランタイム効率に反映されています。 1)Golangのゴミ収集メカニズムは便利ですが、パフォーマンスに影響を与える可能性があります。

seetgolangforhighperformance andconcurrency、ithyforbackendservicesandnetworkプログラミング、selectthonforrapiddevelopment、datascience、andmachinelearningduetoistsversitydextentextensextensentensiveLibraries。

GolangとPythonにはそれぞれ独自の利点があります。Golangは高性能と同時プログラミングに適していますが、PythonはデータサイエンスとWeb開発に適しています。 Golangは同時性モデルと効率的なパフォーマンスで知られていますが、Pythonは簡潔な構文とリッチライブラリエコシステムで知られています。

GolangとPythonはどのような側面で使いやすく、より滑らかな学習曲線を持っていますか? Golangは、高い並行性と高性能のニーズにより適しており、学習曲線はC言語の背景を持つ開発者にとって比較的穏やかです。 Pythonは、データサイエンスと迅速なプロトタイピングにより適しており、初心者にとって学習曲線は非常にスムーズです。

GolangとCにはそれぞれパフォーマンス競争において独自の利点があります。1)Golangは、高い並行性と迅速な発展に適しており、2)Cはより高いパフォーマンスと微細な制御を提供します。選択は、プロジェクトの要件とチームテクノロジースタックに基づいている必要があります。

Golangは迅速な発展と同時プログラミングに適していますが、Cは極端なパフォーマンスと基礎となる制御を必要とするプロジェクトにより適しています。 1)Golangの並行性モデルは、GoroutineとChannelを介した同時性プログラミングを簡素化します。 2)Cのテンプレートプログラミングは、一般的なコードとパフォーマンスの最適化を提供します。 3)Golangのごみ収集は便利ですが、パフォーマンスに影響を与える可能性があります。 Cのメモリ管理は複雑ですが、コントロールは問題ありません。

speed、効率、およびシンプル性をspeedsped.1)speed:gocompilesquilesquicklyandrunseffictient、理想的なlargeprojects.2)効率:等系dribribraryreducesexexternaldedenciess、開発効果を高める3)シンプルさ:


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

WebStorm Mac版
便利なJavaScript開発ツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

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