gpt4 book ai didi

go - 直接从命令行运行但在 VS Code 上导致 "Not able to determine import path"的简单 Go 代码

转载 作者:行者123 更新时间:2023-12-01 22:21:10 28 4
gpt4 key购买 nike

这是我第一次用 Go 编码。我正在关注一个示例,并且可以成功运行下面的小应用程序。但我找不到没有在 Visual Studio Code 中运行的原因。到目前为止,我可以看到,我遵循 this answer 中的建议说:“......由于你的包在 $GOPATH 之外,你可能需要创建一个模块文件。你需要使用”来初始化你的 go 模块。
去.mod

module jimis.net/snippetbox

go 1.15
main.go
package main

import (
"log"
"net/http"
)

func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", home)
mux.HandleFunc("/snippet", showSnippet)
mux.HandleFunc("/snippet/create", createSnippet)
log.Println("Starting server on :4000")
err := http.ListenAndServe(":4000", mux)
log.Fatal(err)
}
处理程序.go
package main

import (
"fmt"
"net/http"
"strconv"
)

func home(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.Write([]byte("Hello from Snippetbox"))
}
func showSnippet(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil || id < 1 {
http.NotFound(w, r)
return
}
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
}
func createSnippet(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
http.Error(w, "Method Not Allowed", 405)
return
}
w.Write([]byte("Create a new snippet..."))
}
启动.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
}
]
}
输出
Starting linting the current package at c:\WSs\snippetbox\cmd\web
Starting "go vet" under the folder c:\WSs\snippetbox\cmd\web
Starting building the current package at c:\WSs\snippetbox\cmd\web
Not able to determine import path of current package by using cwd: c:\WSs\snippetbox\cmd\web and Go workspace:
C:\WSs\snippetbox\cmd\web>Finished running tool: C:\Go\bin\go.exe vet .

C:\WSs\snippetbox\cmd\web>Finished running tool: C:\Go\bin\go.exe build -i -o C:\Users\DEMETR~1.EXT\AppData\Local\Temp\vscode-go7pNgfo\go-code-check .
项目结构(pkg 和 ui 文件夹为空)
tree
VS 代码 settings.json
{
"workbench.startupEditor": "newUntitledFile",
"editor.minimap.enabled": false,
"go.formatTool": "goimports",
"explorer.confirmDelete": false,
"go.toolsEnvVars": {

"GO111MODULE": "on" /// *** I tried both on and off with no changes on my issue
}
}
去环境
PS C:\WSs\snippetbox> go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\aa.bb.ext\AppData\Local\go-build
set GOENV=C:\Users\aa.bb.ext\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\aa.bb.ext\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\aa.bb.ext\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\WSs\snippetbox\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\DEMETR~1.EXT\AppData\Local\Temp\go-build201665690=/tmp/go-build -gno-record-gcc-switches
PS C:\WSs\snippetbox>
如果相关,我将此扩展添加到 VSCode
Visual Studio Code v0.16.1 丰富的 Co 语言支持
但是从命令行一切都很好
PS C:\WSs\snippetbox> go run ./cmd/web
2020/08/20 18:19:51 Starting server on :4000

最佳答案

感谢 Go Slack (GOPHERS) 中的讨论,有人指导我找到了这个解决方案。希望它可以帮助 future 的读者。
1 - 添加程序和 cwd 以启动

{
"version": "0.2.0",
"configurations": [

{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"env": {},
"args": [],
"program": "${workspaceRoot}/cmd/web",
"cwd": "${workspaceFolder}"
}
]
}
2 - 代码必须在 GOPATH floder 之外
3 - 添加 go.useLanguageServer": true (C:\Users..\AppData\Roaming\Code\User\settings.json)
{
"workbench.startupEditor": "newUntitledFile",
"editor.minimap.enabled": false,
"go.formatTool": "goimports",
"explorer.confirmDelete": false,
"go.useLanguageServer": true,
"go.toolsEnvVars": {

"GO111MODULE": "on"
}
}
4 - 这是我当前的 GO 扩展,虽然我不知道每个扩展与我的解决方案的相关性如何
PS C:\> code --list-extensions
...
casualjim.gotemplate
dunstontc.vscode-go-syntax
golang.go

关于go - 直接从命令行运行但在 VS Code 上导致 "Not able to determine import path"的简单 Go 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63513324/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com