Go安装成功,但简单的Hello World没有输出。

huangapple go评论112阅读模式
英文:

Go installed successfully but no output with simple Hello World

问题

使用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扩展设置settings.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 run main.go
预期输出:
> Hello world
实际输出:
终端停止响应(即使没有time.Sleep),然后没有任何输出。

尝试的修复方法

  1. 尝试使用go build,然后运行生成的可执行文件,但没有收到输出。
  2. 尝试构建并将输出重定向到文件:
    go build main.go
    .\main.exe > output.txt
    output.txt文件被创建,但是内容为空。
  3. 检查控制台编码:Active code page: 65001
  4. 禁用防病毒软件或安全软件:我正在使用标准的Windows安全功能,没有安装第三方安全软件。
    我甚至尝试重新安装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或构建后运行仍然没有输出。仍然没有得到输出。

英文:

Using Windows 11 Pro, installed Go using the installer.
Here is my main.go file:

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("Hello world")
	time.Sleep(2 * time.Second)
}

And all relevant info I can think of:
Powershell version:
PS C:\Users\fares.alghazy\go\src> $PSVersionTable.PSVersion

Major Minor Build Revision


5 1 22621 1778

GO extension settings.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": {
    
    }
}

System/Directory Info:

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 

The problem:

Action run:
go run main.go
Expected output:
> Hello world
Received output:
Terminal stalls (even without the time.sleep) then nothing happens

Attempted fixes:
1- Attempted to use go build then running the exe, no output was received
2- Attempted to build then redirect the output to a file:
go build main.go
.\main.exe >output.txt
output.txt was created but empty
3- Checked console encoding : Active code page: 65001
4-Disable Antivirus or Security Software: I am using standard windows security features, no 3rd party security software installed
I have even tried re-installing the Go software, restarting my pc, restarting VS code, etc
Why is my code not showing output? What can I do to fix it?
Update 1:
As instructed by @mkopriva:
go mod init my_app produce the following go.mod file:

module my_app

go 1.20

My new directory is:

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

No changes with go run main.go, or with building then running. Still not getting an output.

答案1

得分: 4

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文件夹。你还应该将GOPATHGOBIN从防病毒扫描中排除,因为你可能也会在那里构建和运行代码。

英文:

Go programs are often falsy recognized by anti-virus software to be malicious, even though they are not. There is even a FAQ entry on this topic in the official Go documentation.

Windows defender will often delete your executable file right after compilation. Instead of using go run main.go you can instead take the two steps separately by first calling go build main.go and then trying to run main.exe. This will fail when Windows deletes your executable right after creation. I have seen compiled DLLs disappear after over a minute, when I thought I was safe, so beware that the executable might not be deleted right away, but maybe seconds or even minutes after compilation.

When you call go run main.go it will build the executable in a temporary folder under your %APPDATA%'s Local path, in a folder called go-build.

You might want to create an exception rule in your Windows defender to ignore go-build folders in your APPDATA. You should also exclude GOPATH and GOBIN from anti-virus scanning, you probably will build and run code there as well.

huangapple
  • 本文由 发表于 2023年7月28日 16:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76786165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定