Installing and Running Your First Program in Golang

avinashtechlvr

Avinash Chodavarapu

Posted on March 19, 2023

Installing and Running Your First Program in Golang

In this blog post, we will show you how to write and run your first Go program.

Image description

Installing Go

To install Go on your system, you need to download the binary distribution for your platform from the official website. Alternatively, you can use a package manager such as apt or brew to install Go. You can check if Go is installed correctly by running go version in your terminal.

Writing your first Go program

To write a Go program, you need to create a file with the .go extension and write some code in it. For example, let's create a file named hello.go and write the following code:

package main

import "fmt"

func main () {
    fmt.Println("Hello, World!")
}

Enter fullscreen mode Exit fullscreen mode

The first line package main tells Go that this is a standalone executable program rather than a library. The next line import "fmt" imports the fmt package which provides formatted I/O functions like Println. Finally, we define the main function which is the entry point of our program. It simply prints the string "Hello, World!" to the console using the fmt.Println function.

Running Your Go Program

Once you have written your program, save the file and open your terminal. Navigate to the directory where you saved the hello.go file and run the following command:


$ go run hello.go
Hello, world!
Enter fullscreen mode Exit fullscreen mode

This command will compile and execute your code without producing any intermediate files. If you want to compile your code into an executable file that you can run later, you can use the go build command instead:


$ go build hello.go
$ ./hello
Hello, world!
Enter fullscreen mode Exit fullscreen mode

Finding more packages

Go has a rich standard library that provides many useful packages for common tasks such as networking, testing, cryptography and more. You can find documentation for these packages on the official website.

You can also use external packages that are developed by other developers and hosted on online platforms such as GitHub or Bitbucket. You can find these packages using the package discovery tool, which allows you to search by keywords or browse by categories.

To use an external package in your code, you need to import it using its full path (for example: "github.com/user/package"). You also need to download it using the go get command before running or building your code.

Some popular tools and software developed using Go include:

  • Docker: a containerization platform that allows developers to package and deploy applications in a self-contained environment.
  • Kubernetes: an open-source platform for container orchestration that automates the deployment, scaling, and management of containerized applications.
  • Prometheus: a monitoring system and time series database that collects metrics from various sources and allows you to query and visualize them.
  • Terraform: a tool for building, changing, and versioning infrastructure using code.

These are just a few examples of the many tools and software projects that have been developed using Go. The language's simplicity, performance, and concurrency features make it a popular choice for building high-performance distributed systems and cloud-native applications.

Conclusion

In this blog post, we have showed you how to write and run your first Go program. We have also briefly discussed how to find and use more packages in your code.

Go is a powerful and expressive language that offers many benefits for developers who want to create fast and reliable software. If you want to learn more about, go programming language, we recommend that you check out the official tutorial or the online tour.

💖 💪 🙅 🚩
avinashtechlvr
Avinash Chodavarapu

Posted on March 19, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024