DEV Community

Brian Wieder
Brian Wieder

Posted on • Originally published at bwieder.Medium

Basics of a Go Program

What is Go?

Go is a fast growing language, designed by Google Engineers to take advantage of the features of modern computers, such as multiple cores/threads, while maintains easy to learn and use syntax. Furthermore, in some use cases, Go has been reported to use less memory and CPU when compared to similar programs written in other languages.

Go is being utilized in major projects such as Docker, and Kubernetes, as well as at large companies such as Google, Uber, and Netflix to name a few.

Simple Go Example

First, let’s take a look at a basic Go file to figure out the basics.

package main

import "fmt"

func main() {
   fmt.Println("Hello, World!")
}
Enter fullscreen mode Exit fullscreen mode

This is just a simple program that when compiled and run, will print “Hello, World!” to the command line.

The first line of code in any Go file must be the the package declaration.

package main
Enter fullscreen mode Exit fullscreen mode

This line states the package that this file belongs to. A package allows you to separate your code, usually depending on the context of what that file’s code does. All you need to know for the basics of Go though, is that the package must either be the name of the folder that the file is in, or main. If you name the package main, then it must be an executable. This means that it must contain a main function, code that gets run when the program starts. I will cover the main function a bit later in the article.

Next, on line 4, we have the packages that our code imports/uses.

import "fmt"
Enter fullscreen mode Exit fullscreen mode

Go provides a standard library that contains several packages that we can use, so that we get functionality without having to write the code ourselves. In this case, we are importing the fmt package. The fmt package provides functionality for all things formatting input and output.

Next, we have the main function, which is the function that gets run when the program executes.

func main() {
   ...
}
Enter fullscreen mode Exit fullscreen mode

On line 6, we have the call to the fmt package’s function Println, which prints the text Hello, World! to the console.

fmt.Println("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

Running a Go Program

Now that we have a Go file written, time to run it!

First, we are going to build our program. To do this, we are going to use the go build command. You will want to open a terminal or command prompt in the same directory as your Go file. Also, you will want to take note of the name of your go file.

To build my file, I am going to run this command in my terminal

$ go build -o hello-world BasicsOfGoProgram.go
Enter fullscreen mode Exit fullscreen mode

This command builds my Go file BasicsOfGoProgram.go into an executable called hello-world. In order to build your file, you can replace hello-world with whatever you want your executable to be named, as well as replace BasicsOfGoProgram.go to the name of your Go file.

Finally, to run my program, I will run this command in my terminal

$ ./hello-world
Enter fullscreen mode Exit fullscreen mode

And I will see the output

Hello, World!
Enter fullscreen mode Exit fullscreen mode

In order to run your application, you can replace hello-world with whatever you named your executable when you ran go build.

This allows you to manually build and run your application, however, Go also provides a method of building and running your program in just one command to make development just a little bit easier for you. For example, if I run this command in my terminal, it will build and run my go file

$ go run  BasicsOfGoProgram.go
Enter fullscreen mode Exit fullscreen mode

And I will see the output again

Hello, World!
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

That was a basic introduction to Go, and I hope that you might have learned something if you are new to Go. I plan on making more articles articles about Go and other languages in the future. If you have any suggestions on how to improve this article, or anything else, feel free to leave a comment down below.

Top comments (0)