2

I have no clue what I'm doing wrong, I look up steps online what to do and they all tell me to just go to CMD, type in go get ___ and then just import the github link into my file but I constantly get the could not import error in VS Code.

I am trying to parse some HTML code that I get from a response and I need the "golang.org/x/net/html" to be able to do things. Can someone please tell me what I'm doing wrong?

SnowyCDN
  • 191
  • 1
  • 1
  • 6
  • 1
    Have you set up your project (does it have a `go.mod` file)? If not, you need to run `go mod init projectname`. VSCode gets confused when no `go.mod` file is present, and that file should be in the top directory you opened, it doesn't handle them in subdirectories – xarantolus Apr 06 '21 at 06:08
  • And you should run the `go get ...` command in the same directory where `go.mod` is, that way the package is added as a project dependency. Also maybe see [this](https://golang.org/doc/code) – xarantolus Apr 06 '21 at 06:09
  • @xarantolus Creating the mod file seems to have solved my issue. Thank you very much. – SnowyCDN Apr 06 '21 at 06:15
  • 1
    You are not following https://golang.org/doc/#getting-started. It is basically dead simple. Just set up your module _properly_ (i.e. as explained in the documentation, no shortcuts, no being clever). The run `go get golang.org/x/net/html` and add `import "golang.org/x/net/html"` to your code. Note that you neither have to "download" nor "install" remote packages. After `go get and import everything is done automatically by the go tool. – Volker Apr 06 '21 at 06:46

1 Answers1

8

You can use go modules to manage dependencies.

First you initialize your module with go mod init ProjectName. Then when you build your project with go build it will find and download missing packages to module cache directory which is ~/go/pkg/mod.

You can use go mod tidy also.

For more information: Go Modules