There are additional usages for the replace directive, which can be found in the Go Modules wiki documentation.Īnd the last step would be to tell the IDE to load the dependency for editing alongside our project. The first argument of the replace directive is the original module we want to replace and the second one is the path on the local filesystem to where the module can be found. This directive would look similar to this: “` replace gopkg.in/yaml.v2 => d:/yamlv2 “`. v2 next to our delve clone by using the URL and then check out the code at v2.2.2, the one used by Delve at the time of writing this article.Īfter we have the library locally available as well, what we need to do is to add a new directive at the end of the go.mod file to tell the go command to use the local version of the library instead of the cached one. To make sure all the dependencies are downloaded in go’s cache, we can invoke the go mod download command manually in the built-in terminal, or by opening any file, such as cmd/dlv /main.go and invoking the Sync packages of /go-delve/delve go to Settings/Preferences | Go | Go Modules (vgo) and make sure that the Go Modules check is enabled and if it’s not, enabled it.Īfter we enable the Go Modules support, we can now go back to editing the application and adding the library to the active editor.after the clone operation finishes, open the project.in the URL field use as a value, and in the Directory field, use any directory outside of GOPATH.click on Check out from Version Control option and select Git.There, we can perform the following steps to use a different project Delve, the Go Debugger, as an example: Let’s explore a more complex example by closing the current project and going back to the Welcome Screen. This allows us to replace a dependency in a few different ways, the most important one being that we can work on a local copy of that dependency. To edit dependencies of the current project, Go Modules have a special directive called replace . If you don’t want to download the dependencies twice, as described above, then you can first edit the go.mod file and add the following line at the end of it require /pkg/errors v0.8.0 then go to the Go file and add the import statement “`import “/pkg/errors”“` and invoke the quick-fix to download the dependency. If we want to use version 0.8.0 instead, we can edit the go.mod file manually, which will trigger a refresh of the dependencies and use the expected version. This will run the required Go commands to download and install the desired version of our dependency.Īt the time of writing, /pkg/errors is at version 0.8.1, so this version will be installed as we did not specify anything in our go.mod file. To import this dependency, write the import statement “`import “/pkg/errors”“` and use the Sync packages of /JetBrains/go-samples quick-fix on it. We can use the Simple Application type instead of Empty File to move past the boilerplate part.Īfter writing a small HelloWorld function, let’s say we want to handle errors in our code using the popular /pkg/errors package. Next, creating a Go file is done as before, so nothing changes here. For example, we’ll publish this under /JetBrains/go-samples so others can use the import statement “`import “/JetBrains/go-samples”“` in their code. We can edit the file to change the module name to better suit our needs. While Go 1.10 with the special vgo binary installed can be used, this is not recommended as vgo is not updated as frequently to be on par with the Go releases and might have an unforeseen impact in a mixed Go versions environment.įinally, we can use the Proxy field to specify if we want to download the Go Modules (or packages) directly from the Internet (default setting), or via a proxy system such as Athens or JFrog’s Go Registry in case you want to store the dependencies in a central place for everyone in the team to have access to the same versions in a better-managed environment.Īfter the project is created, we can see that it already contains a go.mod file. We can then specify the location of the project, which can now be set outside of the GOPATH, to any directory in your system.Įnsure that GOROOT points to a Go SDK which is 1.11 or newer. Then choose Go Modules (vgo) as the project type. In this blog post, we’ll explore how to work with Go Modules when creating new Go projects or working with existing ones.įirst, let’s create a new project by selecting New Project from the Welcome Screen.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |