How do I run a GO executable? [closed] - go

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".

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.

Is there a canonical way to add build scripts to a Go project? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 10 months ago.
Improve this question
Coming from the Node.js world, whenever you need a script (such as a build script there), a very common way of handling this is by adding it to the scripts block in the package.json file, so that you can the script by calling npm run <scriptname>. In other words, Node.js (respectively npm) has a built-in way to deal with (simple) scripts.
Does an equivalent to this exist in the Go world?
To put it differently: Suppose I have some commands such as doing a build, cleaning the build directory, creating a new version, and so on, is there a better way to do this than by adding a bunch of (platform-dependant) Bash script files?
In case there isn't – is there at least a common way of where to put these scripts? Or is this all left to the developer?
You can use Makefile
this one is what I use sometimes:
BINARY_NAME=main
build:
go mod tidy
go build -o ${BINARY_NAME} main.go
clean:
go clean
rm ${BINARY_NAME}

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)

Issue on ~/.bashrc, written file PATH is not correct [closed]

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 1 year ago.
Improve this question
I am downloading Go on my Ubuntu 16.04 computer. I am following this tutorial and I can't progress from this part on youtube https://www.youtube.com/watch?v=YS4e4q9oBaU&t=1810s:
When I created that Main.go an error on VSC console shows up:
Failed to run '/usr/local/go/bin/go env' (cwd:/home/santiagoquinteros/code):
Error: Command failed:/usr/local/go/bin/go env GOPATH GOROOT GOPROXY GOBIN GOMODCACHE
missing $GOPATH
If i write on the console for example this command, this shows up:
~$ go get github.com/nsf/gocode
missing $GOPATH
This is whats inside my ~/.bashrc:
export GOROOT=/usr/local/go
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/home/santiagoquinteros/golib
export PATH=$PATH:$GOPATH/bin
export GOPATH=$gopath:/home/santiagoquinteros/code
On the video since 19:10 to 25:15 these lines of code are created and I followed him without making something else, but for some reason VSC is redirecting itself to another PATH I didn't mention. I have created the two folders called "golib" and "code" in the correct directory too.
I have searched on these pages too but all their solutions doesn't help me:
https://golang.org/doc/install
https://gist.github.com/vsouza/77e6b20520d07652ed7d
https://github.com/microsoft/vscode/issues/108003
https://github.com/microsoft/vscode-go/issues/2595
https://askubuntu.com/questions/1238895/go-cannot-find-goroot-directory-error-on-installing-go-in-wsl
Take note that I'm working on a user limited version of ubuntu 16.04 and this could be the main issue.
VSC stands for VisualStudioCode
I am guessing the error message comes from https://github.com/golang/go/blob/release-branch.go1.16/src/cmd/go/internal/modload/init.go#L207-L210
In the last line of your ~/.bashrc, you meant export GOPATH=$GOPATH:..., not export GOPATH=$gopath:.... Since $gopath is not set, your GOPATH would end up being :/home/santiagoquinteros/code. The above code doesn't like it.
As others said, you don't need to set GOROOT. With go1.16, the module mode is the default, so you probably don't need to set GOPATH but live with the default GOPATH which is $HOME/go.
Many things have changed recently. I recommend newer sets of materials like https://golang.org/doc/#getting-started, https://learn.go.dev, https://play-with-go.dev/ , ...

Install packages locally in golang module feature [closed]

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

Resources