Golang, how to build and run your first Golang application is meant to be a simple introduction to building and running a simple Golang application. We will also cover some information about modules and a general overview of the building, running, and executing Golang apps.
Specifically, we will cover: go run, go build, and go install. Let’s jump into it.
Golang, how to build Golang applications and use the run and build commands.
Specifically, we will cover: go run and go build. We will cover syntax, usage, examples, and some example code you can run. This tutorial expects you to have a local golang development environment setup. If you don’t have one or want to follow along exactly you can check out our golang development setup here https://exploitbrokers/programming/golang/golang-setup-visual-studio-code-for-windows/.
GO example file
Here is the example file we will use to show how to use the build and run commands. This is a simple application to print out a simple message.
package main
import "fmt"
func main() {
fmt.Println("Hello, i'm learning golang from exploitbrokers.com")
}
You should copy and save this to any file name you want but it must end in a .go extension. It should look something like
helloExploitBrokers.go
GO build
We will first learn how to build golang applications using the build command built in to go. The “build” command does pretty much what it sounds like, it builds an executable that can be run at a later point in time.
go build helloExploitBrokers.go
The go build command will create an executable but needs the file name to be specified.
If we want “go build” to work without explicitly calling the file name explicitly then we will need to set up a go.mod which we will go over later in the article.
So if we run the command above in a windows environment we will get a file that looks like:
helloExploitBrokers.exe
We can then execute the helloExploitBrokers.exe and get the following output. To execute you can use a terminal, command prompt, or if you’re in visual studio code you can use the interactive. See the image below.
Executing the executable from an interactive terminal/command prompt/etc is as simple as:
./helloExploitBrokers.exe
The execution above will print out our message:
Hello, i'm learning golang from exploitbrokers.com
Now we will learn how to make a go.mod file that allows visual studio code to run it instead of using the terminal. It’s important to note that any console apps needs console input from the user will still need to be run similar to our current application as only interactive terminals allow user input when using visual studio code.
GO Mod Init
Go’s “mod init” command allows us to set up a go.mod file which makes our current file act as a module. A module is go’s way to have packages. If you’ve ever used packages in other languages the concept is the same.
The syntax and sample usage is as follows:
go mod init helloExploitBrokers
Once the command is ran you will notice a new file will appear.
go.mod
The file go.mod will contains something similar to the following.
module helloExploitBrokers
go 1.16
This tells go that this module is named helloExploitBrokers and has a go version of 1.16. The technicalities are beyond the scope of this tutorial and are best looked up at a later point. The big takeaway is that now using’s visual studio code’s built-in run will work now.
Go Run
Go’s run command makes it easier to run our code when in our ide because it essentially makes a temporary exe and runs it for us. It saves time by running the code instead of building it and making us run it manually.
The run syntax will differ slightly depending on whether you have the go.mod file setup.
If you have the go.mod file setup then you can run the following command.
go run .
The . (dot) tells go to look in the current module for the main function we set up in our earlier file.
If you don’t have the go.mod file setup then you can run the following command.
go run helloExploitBrokers.go
The main difference is whether you specify the file or go looks for the main function is the files.
Leave a Reply