This post shows how to run your Golang go script and convert it as an exe file in Windows.
Install Golang for Windows
I am running a 64-bit Windows 10 box.
First, Download Golang Installer (exe file)
Proceed to the download page and download the appropriate version for your system. For this post, we will use a Golang installer. Alternatively, we can use the Golang playground available online if we cannot install Golang on our system.
Then, choose Go for Windows by clicking the “Microsoft Windows” download link.
Once the installer download completes, install it to your local machine. Just click your way through the installation process.
Then, Verify Installation
There is no particular configuration or settings during the installation or even after that. Open your Windows command-line prompt to verify Golang’s version.
Why is my prompt different? Please see DOS – Change your command line prompt.
Golang “Hello World” from .go and .exe
Here are short Golang codes that display “Hello World” on the command-line prompt from a go and exe files.
1 2 3 4 5 6 7 | package main import "fmt" func main() { fmt.Printf("hello, world\n") } |
1. Run Golang Codes as a go file
The filename for our Go codes is helloworld.go
To run a Golang go file, use the following command.
1 | go run helloworld.go |
2. Run Golang Codes as an exe file
Before running our Go program as a .exe, we need to convert it first to exe explicitly. First, you need to point the environment variable GOBIN to your .go file’s path.
1 | set GOBIN=c:\Users\somewhere\blog\Go\01-hellow world |
Then, in the same directory, run the following command.
1 | go install helloworld.go |
The command will generate a helloworld.exe file in the current directory.
Run it now
We have successfully compiled and run our Golang codes as go and exe files.