教程第一步 微软 Go

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

Tutorial first steps Microsoft Go

问题

我正在学习Go语言,按照这个Microsoft教程进行学习。程序运行并显示结果与教程中一样,但是标记给我的两个问题让我感到担忧。我不想在不理解这个细节的情况下继续学习,希望有人能告诉我这是什么原因,是否有其他人也遇到过这个问题,或者帮助我了解问题的原因,我将非常感激。

package main

import (
    "fmt"
    "github.com/x0z38/calculator"   
    "rsc.io/quote"
)
func main(){
    total := calculator.Sum(3, 5)
    fmt.Println(total)
    fmt.Println("Version: ", calculator.Version)
    fmt.Println(quote.Hello())
    
}

我给你留下了在编辑器中标记错误的红线的图片:

教程第一步 微软 Go

我给你留下了这两个问题的图片:

教程第一步 微软 Go

根据我理解的情况,它在任何提到的路径中都找不到这些文件,但是这两个文件我确实放在这个路径下:C:\Projects\Go\src。
我的GOPATH环境变量是:C:\Projects\Go

英文:

I am starting to learn Go following this Microsoft tutorial. The program runs and displays the result as in the tutorial, but the two problems that it is marking me cause me concern. I do not want to continue without understanding what this detail is due to, someone who has also happened to it, or who helps me to know what it is due to, I will be very grateful.

package main

import (
    "fmt"
    "github.com/x0z38/calculator"   
    "rsc.io/quote"
)
func main(){
    total := calculator.Sum(3, 5)
    fmt.Println(total)
    fmt.Println("Version: ", calculator.Version)
    fmt.Println(quote.Hello())
    
}

I leave you the image where the error is marked in red lines in the editor:

教程第一步 微软 Go

I leave the image of the two problems:

教程第一步 微软 Go

According to what I understood is that it does not find those files in any of the mentioned paths, but both files if I have them inside this path: C:\Projects\Go\src.
My GOPATH environment variable is: C:\Projects\Go

答案1

得分: 1

Golang有两种管理依赖的方式:旧方式和新方式。通常情况下,它们之间的切换是自动完成的。

Visual Studio Code尝试使用旧方式检查依赖项。但是我看到你有go.modgo.sum文件,这意味着你正在使用新方式(Golang模块系统)。

环境变量GO111MODULE用于在依赖控制模式之间进行切换。它有三个值:autoonoff。默认值是auto

你看到的只是一个语法高亮问题,而不是编译或执行错误。

英文:

Golang has two ways to manage dependencies: old and new. Switching between them is usually done automatically.

Visual Sudio Code tries to check for dependencies using the old way. But I see you have go.mod and go.sum files which means you are using the new way (the Golang module system).

The environment variable GO111MODULE is used to switch between dependency control modes. It has 3 values: auto, on, off. The default is auto.

What you see is just a syntax highlighting problem and not a compilation or execution error.

答案2

得分: 0

好的,这是我做的事情:

首先:按照Mushroomator在我的根项目中建议的方式运行go mod tidy,但没有起作用。

其次:删除GOPATH环境变量,因为正如JimB所评论的那样,它不再使用,但也没有起作用。

现在,我感到有点困惑,因为也许我按照他们要求的做了,但我不知道如何解释。

所以,它现在可以工作了,不再显示语法错误。删除GOPATH环境变量,设置GOMODULE111=on,并将我所有文件所在的Projects/Go目录移到桌面上,并标记这个语法错误:

教程第一步 微软 Go

Google
Todo
Imágenes
Noticias
Shopping
Vídeos
Más
Herramientas

Cerca de 1,260,000,000 resultados (0.39 segundos)
Español
Inglés

现在,正如你所看到的,我直接在helloworld目录中打开VS Code,语法错误消失了,但我希望能直接从src目录打开,以便查看我到目前为止学到的所有内容:

教程第一步 微软 Go

英文:

Ok this is what I did:

First: Run go mod tidy as suggested by Mushroomator in my root project, and it didn't work

Second: remove the GOPATH environment variable, since as JimB commented it is no longer used, and it didn't work either.

Now, this is where I feel a bit confused, because maybe I did what they asked me to do but I don't know how to explain it.

So, it works and no longer shows a syntax error. Delete the GOPATH environment variable, SET GOMODULE111=on, and take out the Projects/Go directory where I have all the files and put it on the desktop and mark this syntax error:

教程第一步 微软 Go

Google
Todo
Imágenes
Noticias
Shopping
Vídeos
Más
Herramientas

Cerca de 1,260,000,000 resultados (0.39 segundos)
Español
Inglés

Now, as you can see, I open VS Code directly in the helloworld directory, and the syntax error disappears, but I wanted to have it open directly from the src directory to see everything I've learned so far:

教程第一步 微软 Go

答案3

得分: 0

我了解到你想让VS Code(或gopls)正确识别多模块项目。请参考这个链接(你正在使用Go1.18)。

还有关于go modules的更多信息。

以及go workspace

希望这些能对你有所帮助。

英文:

What I have learned is that you want VS Code (or gopls) to correctly identify a multi-module project. Please refer to this (your are using Go1.18).

And more about go modules.

And go workspace.

Hope those can help you.

huangapple
  • 本文由 发表于 2022年3月28日 23:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/71650225.html
匿名

发表评论

匿名网友

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

确定