Getting Started With Go

Hi, I am Vivek, a 26 years old developer from India. I love making apps. You can check my github for some of my open source projects.
Search for a command to run...

Hi, I am Vivek, a 26 years old developer from India. I love making apps. You can check my github for some of my open source projects.
No comments yet. Be the first to comment.
In this series, I'll be posting articles to get you started with Google's GoLang.
Variables are containers that hold some value. There are two types of variables in Go, one whose value can be changed and the other whose can not we call them constants. We can define a variable using the var keyword followed by the variable name and...
Migrating a production database is often a high-stakes operation. Traditional "dump and restore" methods require significant maintenance windows, leading to service downtime. To solve this, PostgreSQL offers Logical Replication, a native mechanism th...

Ever found yourself in the endless cycle of "delete Docker image, redeploy, repeat" to keep your Dokku apps updated? You’re not alone. If your Dokku-deployed application relies directly on a Docker image (perhaps from Docker Hub or your private regis...

Lately, my internet has been throwing tantrums — not because the connection itself is bad, but because every time the power blinks, my router decides it's time for a little nap. Naturally, the obvious

So, one day I saw my old laptop sitting there, collecting dust like it was auditioning for a role in an apocalypse movie. And then it hit me: "Why not turn it into a server?" There was just one minor hiccup. Its Ethernet port was as dead as my dreams...

Recently I was tasked with setting up kubernetes cluster with an Nginx Ingress. As a newcomer to Kubernetes, I tackled this setup and learned a thing or two along the way. Let me walk you through my experience and share some helpful tips. The Goal: S...

In this post, I'll walk you through setting up your go project. As a prerequisite you must have go installed in your system, you can get it for your system from https://go.dev/dl/
Now that that's out of the way, go ahead and create a directory called my_project, Inside the directory you need a go.mod at the root of the project, this file that keeps track of the modules that provide those package (this of it as package.json file if you're coming from Java Script). You can create the go.mod file using go mod init module_name command, you can use your own module name, typical practice is to use your version control repo path for ex github.com/iamvivekkaushik/my_project. That is if you want to publish your module for others, the module path must be a location from which Go tools can download your module.
Now create a main.go file that will contain our go code and paste the following code.
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
To run your program use:
$ go run main.app
Hello World!
When developing an executable program, we use the package main. The package “main” tells the Go compiler that the package should compile as an executable program instead of a shared library.
We will separate our source code into separate packages, this is to enable re-usablity of the code. The naming convention for Go package is to use the name of the system directory where we're putting the source file, the package will be same for each file within a directory.
After defining the package we use the import statement to import a package called fmt, probably short for formatter. The package fmt comes from the Go standard library. When we import packages, the Go compiler will look on the locations specified by the environment variable GOROOT and GOPATH.
Then we define a main function using the func keyword and use the "fmt" to print the "Hello World!" text.
If you're coming from JS you might be wondering I never called the main function (we will learn about functions in the coming posts) how is it running. So the main function is actually automatically called when you run the program, this is called the entrypoint of your project. You must have a main function defined in your main package.
Here are some commands to help you get started.
To add missing module requirements or remove unneeded requirements, use:
go mod tidy
To add a third party package:
go get <module_url>