Hey folks! This is the first tutorial in our golang tutorial series. In this blog, we will have an introduction and will create a basic hello world program. Let's get started!
Introduction
Go was developed by Google and it's a compiled and statically typed language. Some of the great big projects made using go are:
Kubernetes
Docker
Dropbox (migrated from python to go)
Advantages of Go
Simple syntax
Have some powerful tools made for it.
Faster compilation
Statically typed
Automatic Garbage collection
Opensource โค๏ธ
Pain Points
Go is not purely Object Oriented.
Error handling can sometimes be painful.
Its standard library is not the only thing that we need.
Installation
We can play around with go in a sandboxed environment at https://go.dev/play/ but I urge you to install it on your local system from https://go.dev/doc/install . After going through the steps you can verify your install by
go version
Hello World
I would recommend you use VSCode as the code editor as we can install some useful go extensions there.
Now create a directory in your system and in this directory create a subfolder named hello-world and in that subfolder create main.go . Replace the content of your file with this
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Now let's look at different ways to run this program -
go run {filename} : In your terminal cd into the hello-world folder and run
go run main.go
this would print
Hello World!
go build . : This will create a binary executable in the folder which we can execute using ./{name} (name is the name of that executable file same as the name of our folder).
go install : Similar to go build but the executable file is created inside the bin folder of your workspace.
Explanation of Hello World program
package name : Every file must be a part of a package. Here the package name is main which is a special package as it tells the compiler that the following program should be compiled as an executable.
import : To import different packages. We have imported the "fmt" package.
func main(): This is to define a function with name main. The main function should always reside in the main package. It's the entry point to the program.
fmt.Println("Hello World!"): We have used the Println() function from the fmt package which is used to print text to the standard output.
Signing-off ๐
Hope you like this blog, there is a lot to come, I will post blogs of this series regularly. So please follow me. Open for your suggestions in the comments section. If you have made so far please at least like the blog.
Also please connect with on Linkedin and Twitter.