Golang run on Windows without deal with the Firewall - go

I'm working on a Rest API with Go, but everytime I try to run my application with
go run main.go
the Windows Firewall tells me that has blocked some features of my app. I would like to know if there's some way to make my executions without have to Accept everytime.

Change
http.ListenAndServe(":3000", r)
to
http.ListenAndServe("127.0.0.1:3000", r)

If you are calling go run main.go following is happening:
your programm is compiled inside a temporary folder
the compiled binary is executed
But the temporary folder is just for one execution. So the next time when you run your programm via go run another folder is used.
The windows firewall does give you always the information which path your server has and if you remember the paths after each time you will see that there is always a different path.
The windows firewall is so configuread that it remembers the path of each programm. So when the path is changing you will always need to comfirm that the new path is allowed to run on that port.
To fix this you should compile your server. Just run go build and exeute the binaries then inside you project folder. Then you will just have to accept just one time.

Hi I had the same problem:
Try that:
Go to Windows Defender Firewall, in Left side menu you saw Inbound Rules click there, then Right Side menu you will see New Rule... click.
Choose Port opened from window -> Next
Select TCP, then define which ports you want I choose 8080 click Next again, Choose Allow the connection Next, Check All Next, Give any Name Goland or anything you want and press Finish. Thats it

based on #apxp answer
in windows cli this works for me
go build main.go && main.exe

this work for me
go build main.go && .\main.exe
and run using makefile

I think #apxp's answer is the complete explanation of the situation; some time after ask this question I found a way to run my application with:
go build -o ejecutable.exe ; if($?) { .\ejecutable.exe }

Just go to your Windows Firewall notification settings:
Control Panel -> Windows Defender Firewall -> Change notification settings
Uncheck the option for Notify me when Windows Defender Firewall blocks a new app to prevent it from showing the popup.

You can use CompileDaemon to auto re-build your project on file changes, because it runs a build anyway, you'll only have to accept once. Plus, your project will re-build automatically!
Install:
go get https://github.com/githubnemo/CompileDaemon
Example usage:
# Assuming your project looks like and you're in your project working dir
# hello/
# hello.go
# Rebuild on .go file edits and run the hello command after
CompileDaemon -command="./hello"

The WSL run under VM, so you have to execute ifconfig
You will see your IP in the section (eth0:) inet x.x.x.x
This x.x.x.x is the IP you have to put in your browser.

Related

How can I change the default output folder path of the GO file?

Because I had installed the Avira in my computer, once I run my go file in GoLand, Avira will prompt me a security alert that "BLOCK HEUR/APC(could) ........."
Meanwhile, GoLand also shows that
Error running 'go build test.go': Cannot run program "C:\Users\Simon\AppData\Local\Temp___2go_build_test_go.exe" (in directory "E:\Application software\GO\awesomeProject"): CreateProcess error=5, ACCESS DENIED.
I want to know how can I change the default output path (i.e., build -o "path") to another path; after all, I don't want to close the realtime protection of Avira in each time, and ā€˜Cā€™ disk is also a sensitive area. Should I configure the settings of the GO source or just change some settings in GoLand? B.T.W, unless necessary, I don't want to change the settings of Avira :)
Thanks in advance, I am a beginner of Go language.
You can edit the run configuration via Run | Edit Configurations... and set the Output directory to whatever location you'd like to, see the screenshot below.
If you wish to set this for all new run configurations of the project, then go to Run | Edit Configurations... | Templates | Go Build and then configure the Output directory setting there. All new Go Build configurations will then use that setting.

Access denied executing compiled program

Using Windows, my Hello World code is in src\hello\hello.go.
When using the command "go run hello.go"
Getting Error like this
# command-line-arguments
C:\go\pkg\tool\windows_amd64\link.exe: cannot create $WORK\b001\exe\a.out.exe: open $WORK\b001\exe\a.out.exe: Access is denied.
This can be caused by an anti-virus application like AVG or G-Data.
You can use the -o argument with go run to specify the output directory, and add that path to the exclude list of your anti-virus should this be the cause.
This usually happens when the executable is being edited in some way, or currently running. Check if you have any spare processes of this executable, or in the worst case, try restarting your computer.
Alternatively, it may also be that the linker genuinely does not have write access to the output directory. Make sure that the directory is writable to the user you are compiling your program as.
Adding code folder to the exception list solved the problem.
Windows Security-> Virus and Threat Protection Settings -> Exclutions->Add folder
Add your workplace folder here where your code exists. Adding temp folder didn't work for me.
go build gotest.go ; .\gotest.exe
Using the above command (regular command prompt.) can eliminate pop-up alerts but don't know the reason.

Gradle :Could not create service of type FileHasher

I'm using Gradle to build a java project.
When I run any task (assemble, test).
I get randomly an error :
Could not create service of type FileHasher using
GradleUserHomeScopeServices.createCachingFileHasher()
Did any one had the same issue before?
Gradle V:3.5
java 8
I'm using the java plugin.
Thanks,
I was facing the same because I accidentally hit ctrl+z during build, and then the error was the same as yours.
I tried to remove the lock file but it didn't solve the problem.
Then I find all the process related to gradle by ps aux | grep gradle, and then kill -9 <pid> them all. The build backed to normal.
In your terminal type this:
./gradlew build
and the outcome would be:
FAILURE: Build failed with an exception.
* What went wrong:
Could not create service of type FileHasher using GradleUserHomeScopeServices.createCachingFileHasher().
> Timeout waiting to lock file hash cache (/Users/zra/.gradle/caches/4.1-rc-1/fileHashes). It is currently in use by another Gradle instance.
Owner PID: 17571
Our PID: 26055
Owner Operation:
Our operation:
Lock file: /Users/xxx/.gradle/caches/4.1-rc-1/fileHashes/fileHashes.lock
now do:
rm /Users/xxx/.gradle/caches/4.1-rc-1/fileHashes/fileHashes.lock
and build again.
P.S replace xxx with whatever username you've.
What worked for me was removing the lock file like suggested in the answer above: rm /Users/xxx/.gradle/caches/4.1-rc-1/fileHashes/fileHashes.lock and also killing the IDLE processes by running the command
./gradlew --status
It generates a list of gradle processes with status like
PID STATUS INFO
23580 IDLE 5.2.1
23860 IDLE 5.2.1
19058 STOPPED (stop command received)
Then kill one of the idle processes by kill <PID> and run the gradle build again.
One simple cause is that gradle daemon is not started with the same environment variable.
I had the same issue while changing the gradle user home.
Simply stop the daemon:
gradle --stop
change the ownership of .gradle/ using the following command:
sudo chown -R <USERNAME> .gradle/
I couldn't find this info elsewhere, posting in case it helps someone:
I had the same issue while running Android Studio on ChromeOS (on Chromebook).
I had shared a Google Drive folder with Linux (by selecting a folder, right-clicking and selecting Share with Linux) and created my project there. Build failed with this error, and removing the lock file did nothing for me.
Problem was resolved for me by re-creating the project under a 'Linux folder'.
If the OS is Windows.
Try this: gradlew build
Find de PID Process
And in CMD execute this command
TASKKILL /F /PID #PID
In my case, I was keeping the entire project in a folder managed by Google Drive Stream. Since that's a very rudimentary filesystem, Android Studio (or Git or Gradle, I can't tell which) was trying to do something on the FS that Google Drive Stream doesn't allow.
I simply moved the project onto my local drive and that fixed it!
I got the same error when I tried to execute ./gradlew from WSL2 ubuntu.
As suggested by #user16115, I stopped the running gradle in my host machine (windows machine) by issuing command gradlew.bat --stop.
After stopping the gradle in my host machine, I issued the same command ./gradlew in WSL2, gradle worked as expected.
ps: I have mounted my C:/ drive into WSL2 ubuntu.
This was same problem for me before some days and now only it's your or the user reading this solution.
Uninstall full android studio (means- 'after uninstall from control panel, delete folders of Android Studio,')
folders to be deleted:
C:\Program Files -> Android,
C:\Users{user folder name} -> .android & .gradle & {other folders related to Android Studio}
Reinstall form it's .exe and make sure your PC in connected with internet and with proper speed.
Download all SDK files as mention in that and keep downloading in Android Studio only. [do not download form other source- otherwise it will show errors, be careful]
Click on new project and select empty project and give the name and click on finish.
you will redirect to another window and than the main thing starts that is installation of GRADLE so for this you should have high connectivity of internet and that it will download automatically and and and now your are ready to go.
I had the same issue and the solution was to login with the correct user and then run gradle. I was logged in with a user that didn't had rights to run the tasks.
I just moved a project from Download folder (on Mac) to another one
If you try to build an inner project - try to go to root project and call
./gradlew <moduleName>:<command>
The easiest solution in my case wast to run
In case of linux
sudo ./gradlew bundleRelease
if windows then login with the user who has highest privileges.
Because this error occurred due to insufficient permissions

Stuck on Google Home Tutorial

I am working on an intro to Actions on Google tutorial. I made it to page 4 and I am stuck on the "Fulfillment Webhook and Deployment" stage. I put the sample backend code into a Go file called populationai.go. I'm confused as to how to do the commands listed in the "Using ngrok to locally run the Webhook" section in Windows, as they are designed for a different operating system. Should I be doing these steps in the command prompt of Windows in the first place? Thanks.
https://www.programmableweb.com/news/how-to-get-started-google-actions/how-to/2017/01/31?page=4
Here are the steps I'm confused on:
We start up the Go application, which exposes the API Server via go run populationapi.go
$ go run populationapi.go
We start ngrok to expose a secure public tunnel on port 9000 via the following command:
$ ngrok http 9000
Edit: every time I try the "go run populationapi.go" command it says
'go' is not recognized as an internal or external command,
operable program or batch file.
Edit: my go file is located on my desktop. Is the issue the location of the file? The installer put Go distribution in c:\Go.
The error means you do not have Go installed.
You need to install go, also known as Golang to run that example code they provide on Step 4.
Make sure to follow the installation instructions as well.
It was just an example. You can write the code in any language as well.

Tool for Windows that can watch a directory for source code changes and then redeploy server?

Right now I'm having to save my code on local machine, quit node.js server, and restart it to see the changes. What would be great is if I could make it reboot so that whenever I click save on my favorite editor, it will automatically start the restart sequence and load the page on my web browser. Can grunt achieve this?
You could use the "up" tool it'll watch a dir for changes and reboot when it finds them.
On the command line run:
npm install -g up
Then you can use it to start your node app:
up -w -t 500ms -n 1 -p 3000 server.js
where the server.js file is a file that exports your http server
See the "Setup" section in the following link to export your server:
https://github.com/LearnBoost/up/
You don't really want to work that way. Instead setup a version control system (SVN, git, or whatever suits you) then set it up, so that the code is deployed only after you actually commit the code. You don't want to restart your server on every file save, as this will slow you down considerably (and will also result in pushing a lot of buggy code to the server).

Resources