My Journey into Go Programming
Today marked a significant milestone in my learning path as I dove into the world of Go programming. After installing and configuring my Go development environment, I’m excited to share what I learned, including how to build and run a simple "Hello, world!" program, as well as the structure of my Makefile for managing my Go project.
Setting Up the Go Development Environment.
The first step was downloading and installing Go on my Ubuntu Linux system. With my development environment correctly configured, I opened Visual Studio Code, which provides excellent features for Go development. Here’s a summary of my progress:
Initializing the Go Module
Before writing the Go program, I initialized a new Go module using the following command:
go mod init hello_world
The go mod init
command creates a new Go module by generating a go.mod
file. This file is essential for managing dependencies and versioning in Go projects. It ensures that all dependencies are tracked and makes the project easily shareable across different environments.
The go.mod
file for my project looked like this
module hello_world
go 1.23.1
This file specifies the module name (hello_world
) and the Go version used for the project (1.23.1
in my case).
Writing the "Hello, World!" Program
After initializing the module, I created my first Go program, a simple "Hello, world!" script. Here's the code I wrote in a file named main.go
:
package main
import "fmt"
func main() {
fmt.Printf("Hello, %s!\n", "world")
}
This program uses the fmt
package to format and print a greeting to the console. It’s a classic starting point to ensure everything is working as expected in the Go environment.
Building the App
Once the code was written, it was time to build the application using Go’s build tool. The go build
command compiles the source code and generates an executable file.
go build -o helloExercise
This command builds the program and creates an executable file named helloExercise
in the current directory. I then ran the program with the following command:
./helloExercise
As expected, it printed:
Hello, world!
This confirmed that everything was working correctly.
Automating with a Makefile
To streamline the process of formatting, building, and running the program, I created a Makefile to automate these tasks. This allows me to manage the project more efficiently. Here’s what my Makefile looks like.
.DEFAULT_GOAL:=build
BINARY=helloExercise
fmt:
go fmt ./...
vet:
go vet ./...
build:
go build -o $(BINARY)
run: build
./$(BINARY)
clean:
go clean
rm -rf $(BINARY)
fmt: This target formats the code.
vet: This target checks for potential issues in the code.
build: This target compiles the Go program into an executable.
run: This target builds the program and then runs the executable.
clean: This target removes the executable and any cached build files.
To build and run the app, I simply run the following in the terminal.
make run
Next Steps
With my Go environment set up and my first program successfully built, my next focus will be exploring Go’s built-in types and variable declarations. By deepening my understanding of these foundational concepts, I will continue progressing on my journey toward becoming a backend developer and mastering Go.
Subscribe to my newsletter
Read articles from Sahal Imran directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by