Install packages locally in golang module feature [closed] - go

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
When I install a package using go get, in the golang module mode, these are installed in the go root folder.
I would like to be able to install the modules in the same way that npm or yarn does for node.js, global or local.
what would be the right way to achieve this behavior.
Thanks.

As others have mentioned, you can create your module files like this:
go mod init [import path]
After that you can run this command any time to move all dependencies into the vendor directory:
go mod vendor
This is very similar to the behaviour of the node_modules directory.
To build using the dependencies in this directory, you can add -mod vendor to your build command:
go build -mod vendor
This way you can copy your project to any machine without go-getting anything on that other machine.
According to the docs, the -mod=vendor flag is set automatically if the Go version in the go.mod file is 1.14 or higher and a vendor directory exists.
To clean unused dependencies from the vendor directory, you can run
go mod tidy

There is little bit different approach. Go lock versions based on github (gitlab, etc) current commit. When you want to use specific versions of packages and not master all the time you want to use go modules (this is official way to do it, but there are some other tools). You init module in you project
go mod init [possible custom package name]
And everytime you call "go get somepackage" it writes versions it uses to a file called go.mod.
You can read more here

Related

can't install a go module, using get -u and install [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I want to install go-fastping, but when I try using
go get -u github.com/tatsushid/go-fastping, this error gets displayed:
go: go.mod file not found in current directory or any parent directory.
'go get' is no longer supported outside a module.
To build and install a command, use 'go install' with a version,
like 'go install example.com/cmd#latest'
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
so I changed to install:
go install github.com/tatsushid/go-fastping
Try 'go install github.com/tatsushid/go-fastping#latest' to install the latest version
and after I did changed to: go install github.com/tatsushid/go-fastping#latest
it worked but when I try to add it in the code it says that the module is not installed.
Your project most likely doesn't have a go.mod file.
Inside the root of your project run go mod init <module-name> to create a module and then you can run go get -u github.com/tatsushid/go-fastping to add this dependency to your project.

how to get go mod tidy not try to download local packages [duplicate]

This question already has answers here:
what is the purpose of `go mod vendor` command?
(3 answers)
Closed 8 months ago.
I have a go monorepo with a few packages that at one point had an import path like github.com/user/ticketing/tickets/models/connection, but is a private repo and I'd rather not have to depend on the remote repo anyway for deployment on kubernetes/skaffold. I was wondering how when running go mod tidy, I can prevent tidy from trying to download the local packages but instead have everything point to the local packages instead through masking/shadowing. I'm open to a bazel-based solution as well, but I feel like tidy would not be impacted by anything around bazel. Thank you.
If you do not want to depend on the availability of a remote repository the usual practice it to do vendoring of the dependencies. Similar questions have already been asked: what is the purpose of `go mod vendor` command?

How to update Go dependencies [duplicate]

This question already has answers here:
Go update all modules
(3 answers)
Closed 1 year ago.
the go mod tidy adds package dependencies to the go.mod file. How we can update them automatically without manually editing go.mod file, e.g. by removing some entries? For instance, if I use make I want to add a similar command that can update all dependencies of my package/repo and then compile the code with latest version of package dependencies.
In order to update all the dependencies you need to use:
go get -u
And then go mod tidy.
go get -u updates all packages and their dependencies, changing the go.mod.

I can't activate GO111MODULE [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to use go modules, after a lot of searching, I just saw some website that said, enable go111module with this command:
GO111MODULE=on
But it's not a path variable, it does not even exist on my pc, so it shows me
GO111MODULE is not recognized as an internal or external command, operable
program, or batch file.
Some people said it's enabled by default on GO v1.16 but it's not working for me,
How do I understand it's not working?
I use:
go mod init
and
go get <some_packages>
It adds the new package to my go.mod but my code can't recognize it and I have an "Unresolved dependency" error in my go.mod file.
I use Goland to solve my problem using their GUI's and it solved my problem but their settings just work on the current project and don't work globally.
What can I do? (I'm a Windows 10 user, Go version 1.16)
Answer (Update):
Set a path variable like this by yourself:
After that, run the command:
go env
You will have this line at first line:
set GO111MODULE=on
Yes its not a path variable.
It is a go environmental variable.
Yes is is available by default actually.
On your terminal, type
go env | grep "GO111MODULE"
That should show you what that variable is set to
You can try the same with other go env variables eg GOPROXY, GOPRIVATE etc just to satisfy yourself.
The output GO111MODULE="" means it is on (since Go 1.16)

How do I run a GO executable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I was trying not to use the default go workspace, but to create a separate workspace location. So I did the following:
Created a folder, say "/Users/user_name/some_path/go_files/"
Created 3 directories in that folder - src, pkg and bin
Created a go file called "hello_world.go" inside "/Users/user_name/some_path/go_files/src"
Exported the GOPATH variable to "/Users/user_name/some_path/go_files"
Built the executable in 2 ways: go build src/hello_world.go and cd src go build hello_word.go
The above generated 2 executables - 1 in go_files/src and the other in go_files
However, running either of them gives an error saying: package hello_world is not in GOROOT (/usr/local/go/src/hello_world)
I tried to run the executable using go run hello_world. How do I resolve this? How do I run an executable?
Please note that I can do a go run hello_world.go from inside src or a go run src/hello_world.go, but I want to know how to run the executable.
However, running either of them gives an error saying: package hello_world is not in GOROOT (/usr/local/go/src/hello_world)
You haven't actually said what you're doing to provoke this error, but it sounds like you're almost certainly running go run hello_world. Once you've build an executable, Go (the language) and go (the command) are no longer involved. Binaries run independently of their compiler, which may be surprising to you if you're coming from an interpreted language background.
You run any given executable by typing its name (if it's in your path) or by typing the path to it (ie ./hello_world) and then pressing "return".

Resources