DEV Community

Cover image for Complete Go Starter Guide: Setup, Syntax & First Program
RS
RS

Posted on

Complete Go Starter Guide: Setup, Syntax & First Program

Have you ever felt like you want to code and learn a new programming language, but there are so many languages to choose from: Java, Python, Rust, Go? I researched, and Go caught my interest, as highlighted by the JetBrains report here. Go was used by approximately 4.8 million users. Many companies have widely accepted Go for backend development and libraries due to its high performance. There are many advantages of Go, which we will cover in future articles. A bit of background on Go is that it was created at Google, and its first public release was in 2009. Go was created to address issues with large-scale systems at Google, such as slow compilation, complexity, and poor concurrency support. C++ and Java do solve some issues, but the learning curve is much steeper than with developer-friendly, readable languages like Python. The focus was on creating a simple language that would inherit many advantages of languages like C++. By the end of this article, we will have Go installed and will have written our first program.


Installation & Verification

Step 1: Download & install

Head to https://go.dev/dl/ and grab the installer for your OS. Double-click and install once downloaded. The installer will set up Go and configure essential paths. There are multiple ways to get Go; you can use package managers like Homebrew for macOS.

Step 2: Verify Installation

Run the following command in your terminal (bash or command prompt):

go version
>>> go version go1.25.5 darwin/arm64

Enter fullscreen mode Exit fullscreen mode

You should see output like go1.21.5. This confirms that Go is installed and ready to use.


What Are Go Modules?

A Go module is a collection of Go files that includes a go.mod file. This file tracks your project’s name and its dependencies. Previously, this was done via a special workspace (GOPATH), but we don’t need that anymore.

Let’s create one: Open your terminal and run the following commands:

mkdir my-go-app
cd my-go-app
# go mod init <the-go-module-name-you-like>
go mod init my-first-go-project
Enter fullscreen mode Exit fullscreen mode

The command creates a go.mod file. This is now the root of your Go project. All the code and commands will run relative to this folder going forward.

Go mod init

Directory


Nostalgic “Hello, World!”

Inside the folder, create a file named main.go. Open it, add all the following code, and save the file:

package main // Every executable program starts with package main

import "fmt" 
// Unlike Python, where methods like print, round are readily available
// in Go functions are always in a package, and we need fmt to use print text.

func main(){ // The main function is where the program begins
 fmt.Println("Hello, World!") // This line prints the text
}
Enter fullscreen mode Exit fullscreen mode

Running & Building: go run vs go build

go run — For Quick Testing

Why we use it: To quickly check if your code works or not; no extra files are created.

go run main.go
Enter fullscreen mode Exit fullscreen mode

Go Run Command

go build — To Create an Executable

  1. The command will compile your code and create a standalone binary file, which you can share and run anytime, even without having Go installed.

  2. After execution, you will see an executable created in the folder, which you can run and share.

  3. Why we use it: If we want to distribute a program or run it later.

go build main.go


Current Project Layout

You can execute the following command to see what the project looks like. You can add more .go files and play around.

ls -la # use `dir` for Windows

go-app/
├── go.mod       # Your module definition file
├── main.go      # Your source code
└── main.exe     # Your compiled program (if you built it)
Enter fullscreen mode Exit fullscreen mode

Variables and Basic Types

Variables are containers used for storing data. Go is a statically-typed language, which means you must define the type of data that the variable will hold.

Basic Types

  1. string: Text (e.g., “Hello”)

  2. int: Whole number (e.g., 10, -10)

  3. float64: Decimal numbers (e.g., 3.14)

  4. bool: True or False

Three Ways to Declare Variables

Full Declaration (Verbose) — Declare the variable name, type, and optionally assign a value

var name string = "Tom Cruise"
var age int
age = 25
Enter fullscreen mode Exit fullscreen mode

Type Inference (Shorter) — Go will figure out the type based on the value assigned.

var name = "Tom" // Go will figure out it's a string
var age = 30
Enter fullscreen mode Exit fullscreen mode

Short Declaration (Mostly Inside Functions) — Use the := operator. It declares and initialises the variable in a single step.

package main

import "fmt"

func main(){
    name := "Tom"   // String
    age := 25       // Integer
    isActor := true // Boolean
    height := 5.5   // Float64

    fmt.Println(name, age, isActor, height)
}
Enter fullscreen mode Exit fullscreen mode

Putting it All Together

Here’s a sample main.go with all these concepts.

package main

import "fmt"

func main(){
    var name string = "Tom"  // Full Declaration
    var age = 25            // Type Inference
    isActor := true        // Short Declaration (inside main function)
    height := 5.5 // Float64

    fmt.Println(name, age, isActor, height) // using variables

    age = 26 // changing variable
    fmt.Println(name, age, isActor, height) // using variables
}
Enter fullscreen mode Exit fullscreen mode

What’s Next?

We have installed Go, created a project with a module, run our first program, and learned about basic data types and variables.

What you can do:

Try playing with the main.go file.

  1. Change the message in the print statement.

  2. Create new variables and try storing new values in them.

  3. Create an int variable and try storing a string in it. Comment on what happens below 🙂

  4. Use go build to create an executable and send it to someone, and ask them to execute it.

In subsequent articles, we will learn more about the syntax, including conditionals and loops.

If you are stuck or have any questions, drop a comment with the output, and I will try my best to answer them all.

Top comments (0)