Install and run a simple Golang application
Hi all..!!
After not writing an article for more than 2 months, here I will share something new that I learned, namely go lang. in this article I will share how to install and run the simple go lang application.
Go is an open source programming language that is supported by Google and many companies use this language in creating their backend applications. Several large companies use this language in their applications, namely Google, Paypal, American Express, Bitly, Netflix, and many more.
The Go Lang language was first released in November 2009 and in 2020 the Go language started to become quite well known for its fairly easy use, flexibility and fast performance. The go language compiles the code that we have created into machine language so that this can make Go execute our code more quickly.
The way to install the Go Lang language on Linux is as follows :
1. Download the go lang file using the following command :
wget https://dl.google.com/go/go1.21.5.linux-amd64.tar.gz
2. Extract the downloaded file using the following command :
sudo tar -xvf go1.21.5.linux-amd64.tar.gz
3. Move the extracted files to the /usr/local/
folder using the following command:
sudo mv go /usr/local
4. Add Go to our global environment by editing the .bashrc
file located in the home/{user}/
folder
export PATH=$PATH:/usr/local/go/bin
Add the script above to the very last line of the file.
5. Open a terminal and run the following command :
go version
If Go is successfully installed, the Go lang version will appear.
Next, here I will explain how to run a simple “hello world” application using Go Lang. First, create a new folder, for example hello-world-app
. After that, create a file called main.go
, this file is the file that will be run first when our application is run. Next, enter the following code into the main.go
file.
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
The code above is the code to display “Hello World” to the console, you are free to edit the file according to what you want.
Compile the main.go
file by running the following command:
go build main.go
then run the compiled results with the following command:
./main
If successful, “Hello world” will appear on the console.
For the development process, you do not need to compile the main.go
file first and can immediately run the application with the following command:
go run main.go
Note :
When creating an application using the Go Lang, make sure there is a
main.go
file because thismain.go
file is the file that will be run first and the package name must use namemain
.
That’s all I can share about how to install and run the simple go lang application. Hopefully this article can be useful for you and myself. Thank you for reading this article, see you again in my next article.
Let’s get connect : Linkedin