• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About Us

Exploit Brokers

  • Home
  • Hacking Tutorials
  • System Admin
    • Linux
  • Podcasts
  • Hacking Tools
  • Courses

Golang Tutorial – How to build and run golang apps

July 10, 2021 by Exploit Leave a Comment

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 and run golang apps
Golang – How to build and run golang apps

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.com/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.

Right clicking the file opens the menu to find the Open in Integrated Terminal options
How to find the “Open in Integrated Terminal” option

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.

Filed Under: Golang Tagged With: golang, golang tutorial

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

  • GitHub
  • Instagram
  • Medium
  • Pinterest
  • Twitter
  • YouTube

More to See

Exploit Brokers HN09

Freakout Botnet Attacks DVRs, Ricochet Problems, MyKings Botnet, & Twitch Hacked.

October 23, 2021 By Exploit

Golang How To setup VS Code on WIndows

Golang Setup for VS Code on Windows

July 10, 2021 By Exploit

Tags

Ethical Hacking golang golang tutorial Hacking Linux Network Scanner Port Scanner Python System Administration

Footer

  • GitHub
  • Instagram
  • Medium
  • Pinterest
  • Twitter
  • YouTube

Recent

  • Linux How To Guide for Files and Directories
  • Freakout Botnet Attacks DVRs, Ricochet Problems, MyKings Botnet, & Twitch Hacked.
  • Golang Setup for VS Code on Windows
  • Golang Tutorial – How to build and run golang apps
  • Maze ransomware hits Cognizant, More Covid-19 Scams, Pulse Secure vulnerable, ruby libraries contain malware, and more

Search

Tags

Ethical Hacking golang golang tutorial Hacking Linux Network Scanner Port Scanner Python System Administration

Copyright © 2022 · Magazine Pro on Genesis Framework · WordPress · Log in