Hello From The World

Fateh Ali Aamir
3 min readMay 1, 2023

When it comes to learning programming in general, we sometimes tend to ignore the science behind it. As a student of Computer Science, I’ve often ignored the magic within and just copy-pasted code until whatever I wanted to make could be made. Understanding the why behind everything is a good approach to have in life.

A Different Approach

Today, we’re taking a different approach though. We’re diving right into the code. Often time when learning programming, we start by learning about different parts of code separately and when the time comes to connect them, we’re mostly at a loss. I believe there’s a better way to familiarise one with any language and that’s to jump right into it. Take a look at the code below.

package main

import "fmt"

func main() {
fmt.Println("Hello, world!")
}

Well, if you have prior knowledge of programming, you probably would’ve understood some parts of it. We’re gonna break it down line-by-line and understand what’s happening in this essentially basic code.

package main

Packages are a big part of Go. They’re created as a collection of source files similar to what we have in Java. Here, we are using package main which will tell the Go compiler that this file is actually an executable program instead of a shared library. This is the entry point of our executable program and the only part of the larger program that will be executed first.

import "fmt"

This might look familiar to you as well. Here we are ‘importing’ a package (also known as a shared library) just like we do in Python or Java. In C/C++, we tend to use the ‘include’ keyword. The fmt package is used for input, output, formatting of strings and some other stuff. It is one of the most essential packages in Go that will be used a lot. You can check out the documentation here.

func main() {
}

The main function is the program’s entry point (just like the main package). Only the code in the main function will get executed and anything outside of it will not. What that means is that any functions declared outside of the main will have to be called within the main function for them to work. No parameters are returned nor any parameters are passed to the main function and it is called by the compiler itself. Be wary of the curly brackets. Make sure your main code stays inside of it.

fmt.Println("Hello, World")

So here’s when the fmt package comes into play. We use a function from the package called Println and we print the desired string. So as you can see, the parameters of a function must be within the round brackets. And if you’re passing a string to it, it must be contained within double quotes. And now, when you run this program, you’ll get the following output:

Hello, World

Oh, wait, you don’t know how to run the Go code? Not a problem. Just go to your IDE, open up the terminal and write the following command with the name of your file and the .go extension:

go run [filename].go

So this was a very basic explanation of a very basic code in GoLang. Now that you’ve got the gist of things, we’ll be going back to our traditional approach and begin looking at different parts of the GoLang programming language and understanding it along the way.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Fateh Ali Aamir
Fateh Ali Aamir

Written by Fateh Ali Aamir

23. A programmer by profession. A writer by passion.

No responses yet

Write a response