Before Starting
This blog is not a single independent blog, it comes from a series i created "Go (golang) - Beginner". Follow the complete series blog by blog and Golang will be quite natural to you.
I assume you have prior programming knowledge like with any one of these: python, c++, c or java, javascript, ruby etc. Because we dont teach babies here ๐.
I am by no means Golang professional i am constantly learning and teaching other at the same time. Adding value to the developer community.
Let's Start With Installing
Way 1:
No rush! Before any code you must have go lang in your pc\laptop (windows\mac) right? Go here: https://go.dev/doc/install and install the Golang in your machine its really easy just yes yes on every button and done. Respectfully, if installing apps or packages on your pc is hard for you.. then you should enhance your knowledge of pc first then come to programming.
If the install was successful run this command in the command prompt in "windows"
If after running this command you see :
1. version of go (might be different for you)
2. machine type
Then Congrats! Your pc has go installed.
Way 2:
If you don't want to install anything then just go to chrome\ brave\ firefox or whatever browser you use and type "golang online compiler" this will open many website that you can open and start typing golang right away.
I dont think this stuff is even necessary to tell you guys as i think everybody have atleast golang installed on their pc because that why you are "learning" it right? But still fine let's GO!
Behavior of GoLang
We will get to the code but wait! this thing is very important.
Golang is "statically typed" not dynamically typed. So? you might ask.
Well look it behaves like c++, c type languages, but writing it might feel like as easy as python. So this is the behavior of GoLang "Works similar to c++, c and syntax and writing feels like python".
Folder + File Structure & First Program In GoLang
Use VSCode! It is the most easiest way to learn run GoLang i love VSCode.
Here is extensions that you must install before running any Go code in VSCode:
Folder & File Structure:
Look remember this for now in plain english "Make a new folder with a suitable name every time you code something new and always make a main.go
file in that created folder".
For example you will code your "Hello World" program in Golang today. So go any where in the windows\ mac you like and create a folder with some good suitable name like "hello_world" or something like that then create a main.go
file in that folder.
So if you every make new project or any new program in Golang, just create a new folder with suitable name and a main.go
file in that created folder.
main.go
is where you will write you code.
Your First Program:
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
For now just act foolish and run the code in vscode, just do it.
Nice Let's understand it now in next section.
Understanding the code
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
Package main
:
A package is nothing but a fancy name for folder. It means we have a package (folder) that contains a file with certain name. Here the name we gave it is "main", but remember in a folder when we create a main.go
file, it must have a name "main" after the package
keyword. Why? why "main" why can't it be "apple" OR "banana"? well you are right it can be apple and banana but main
has a special meaning. It tells Golang that this file is the "boss" in this folder and this is "main" important file. So again, package main
tells Go that in the package(folder) this file is the "main" file.
Now let's get technical "Only main package is executable" Huh? Have you ever installed a game for your pc? or a application from chrome or other browser. They always have a extension after them which is .exe
. When we double click on that file the file executes OR i can say Run. Just like that this main.go
file which has package main
in it, tells Go that create a .exe
of this package from this file. Executable or .exe
are same things, it means that the entire code is now compiled and a .exe
file is created and we can run it now.
Look when i said make a folder and make a main.go
file it doesn't mean there will only be one file in this folder, we can create multiple 100's and more .go
files in this folder. But there can only be one .go
file with package main
line in this entire folder all other files in this folder must have other package name than package main
. As .exe
is created from this file only and only this main.go
file containing package main can be executed.
Also you don't need to call your main file which contains package main
as main.go
, you can name it whatever you want like spiderman.go
but in GoLang it is convention and good practice to always call it main.go
.
import "fmt"
:
In python we use print(<data to print>)
, in javascript we use console.log(<data to print>)
very straight forward i know.
But to do the similar in GoLang we need to import a another package in our program to print anything to our console. That is "fmt".
fmt
provides many methods which can be used to print data to console.
So remember this "fmt" is mostly 99% of the time imported. Why? well always print some data to console my guy! like variables data or output of a function etc.
func main
:
func main() {
fmt.Println("Hello World")
}
Here func
is a keyword that is used to make a function in Golang. In python we use def
and in javascript we use function
.
main()
is not some random name we can give to any function, in the entire package(folder) that might contain 100's of .go
files, only one file out of all of them can have a function with name main() in it. The the file that that can contain this main()
function is none other than the file that contain package main
in it.
So for a file in any package to become executable it must have:main()
functionpackage main
And remember only one .go
file out of all the files in the package(folder) can have main()
function and package main
.
main()
is the opening door to the this program, Go will come and enter this main.go
file then it will start executing from the main()
function, so whatever code is in the main()
function is executed.
In Go (and many other programming languages), the main
function acts as the entry point for the program's execution. This means that only the code written inside the main
function gets executed by default.
Code outside main
: Code written outside the main
function can exist, but it won't be executed automatically when you run the program. These functions or variables might be used by the main
function or other functions within the same package, but they need to be explicitly called to be executed.
fmt.Println("Hello World")
:
Well here is the last line, it simply uses the fmt package, inside the fmt package it used the most used print function called "Println" (capital P see that).
Then it takes a string and prints it out to the console.
The End
I dont think, there is any other source on the internet who has explained just a hello world program to any one in level of detail and plain english. I made sure i sound as simple and familiar as possible.
Thanks For Reading, Subscribe to my blogs and newsletter though.
Again this is a part of blog series "Go (golang) - Beginner" make sure to keep following it for more GoLang.
Find links to my twitter (X), LinkedIn, Github Here:
https://hashnode.com/@NavrajSingh