Anatomy of GoLang Program

aditya goel
9 min readJul 16, 2022

Question :- Show a simple code written in Go ?

Answer:- We have written a simple program in GoLang :-

Question :- How do we run the code in our project ?

Answer:- We used the following command, in order to run the GO program at our local machine :-

go run <NAME_OF_FILE.go>

So let’s kind of break down this go command right here :- go run can take one or two or three or four, a handful of files.

  • It compiles all the code in those files and then instantly executes the result.
  • So any time we have like one or two files, we say Go run, and then the name of the files, one or more that we want to compile and execute and boom off to the races we go.

Question :- Can we just compile the Program alone ?

Answer:- We also have a command called Go Build.

  • Go Build is very much like Go Run. Go Run is used to compile and immediately execute a program. On the other hand, Go Build is used to just compile a program.
  • At our command line, say we run below mentioned command :-
go build main.go

You’ll see that we do not get any output, but if we now list out all the files and folders inside this directory, you’re going to see a new file called main. So, this is an actual executable file that was built out of our source code of main.go :-

Question :- What are other commands available with GO-CLI ?

Answer:- There are many other commands as well, available with Go-CLI :-

  • go fmt :- It is used to automatically format all the code inside of all of our different files.
  • go install and Go Get :- are two commands that are used to handle dependencies inside of our projects. So, if we want to use code that’s been written by someone else, we can use these commands to get access to it on our own personal projects.
  • go test :- It’s used to run and execute any test files that are associated with the current project.

Question :- What does package main means ?

Answer:- A package is being like a project or a workspace. A package is a collection of common source code files. So, if multiple developers are working on one discrete application, like we’re working on one app right now, we would traditionally be creating one single package.

Question :- How many files can be there in one package ?


  • A package can have many related files inside of it, each file ending with a file extension of GO.
  • The only requirement for every file inside of a package is that the very first line of each file must declare the package that it belongs to.
  • Every file must have the statement package main at the very top, just like our current main.go file does right above.

Question :- Which are the various types of packages in GoLang ?

Answer:- There are two different types of packages :-

  • Executable
  • ReUsable

Question :- Explain in detail, about executable packages ?

Answer :- Executable packages are usually used for actually doing something. We are going to be writing programs that we can run and we can use them to accomplish tasks.

  • This is a package, which when compiled spits out an actual runnable file or an executable file, just like what we saw when we executed the go build command at our command line, as shown above.
  • Remember when we ran go build main.go command, It spits-out this main file right here, which we were then able to run and execute. So, this file right here was created specifically because we created an executable type package.

Question :- How do we know if we are making an executable package or a reusable ?


  • Remember that in the beginning of this blog, we called our package with the name of Main. So, the very first line for us inside of our file said package main.
  • It’s actually the name of the package that you use that determines whether you are making an executable or dependency type package. So, specifically, the word main is used to make an executable type package.
  • So, we took package main, we ran go build on it and it spit out a file called main.

Question :- Explain in detail, about reusable packages ?

Answer :- These ReUsable Packages can be think of as being like code dependencies or libraries.

  • These are packages that are not used to say like double click on and execute, instead, we put in a lot of reusable logic or helper functions or stuff that will just help us reuse some code on future projects in the future.
  • Another crucial point to note here is that, compiling to a resuable package, doesn’t at all gives anything i.e. no executable-file is being created.

Question :- When do we use the reusable packages ?

Answer :-

  • If we are trying to make some library of reusable code.
  • If we wanted to make some project that we can share with our friends, they can use our code on their own project, that’s when we would start using a more specialised package name.

Summary :- Basically, whenever we see the word package main, that means we are making an executable package. Any other name whatsoever means we are making a reusable or dependency type package.

Question:- Is there a relationship between the executable package and Main Function ?

Answer:- Any executable package must always have a function inside of it called Main as well.

As shown in below example, we have a an executable package and inside the file, we have a main function :-

Question :- What does import fmt means in the aforementioned code sample ?

Answer :- The import statement is used to give our package, access to some code that is written, inside of another package.

  • So, saying import “fmt” specifically means give my package main, access to all of the code and all the functionality that is contained inside of this other package called Fmt.
  • Fmt is the name of a standard library package that is included with the go programming language by default.
  • Fmt itself is a kind of a shortened form of the word format.
  • Fmt library is used to print out a lot of different information specifically to the terminal, just to give you a better sense of debugging.

Question :- How does import statements works usually ?

Answer:- To get a better sense of packages and how multiple packages work together in a normal project, we can assume that :-

  • We have got our main package.
  • Around it are a bunch of other packages that are part of the standard library of go by default.

Question :- Do our man package not have access to these other packages by default ?

Answer:- Our main package has access to absolutely no code inside of any of these other packages. We have to specifically use the import statement to form a link from our package to these other ones. So, we would say :-

  • import fmt → This shall form a link from our main package over to Fmt.
  • import math → It would get access to the math package as well.

Question :- Can we only import the standard packages ?

Answer:- NO.

  • We are not only limited to packages that are included in the standard library, we could just as easily use an import statement to require in or import in packages that have been authored by other engineers as well.
  • For example, our main package could import FMT, but it could also import in, say, a package called calculator or uploader that are authored and published by other engineers. Recall that, these would be examples of reusable packages.

Question :- How do we study about these standard packages provided by Go ?

Answer:- We can find some fantastic documentation around all of the standard library packages by visiting This is a list of all the different packages that are included in the standard library.

Question :- Whats that func thing in the below shown code ?

Answer:- The first thing I want to talk about is that func thing inside of our main go file i.e. func Main()func is short for function inside of go function, just like functions in other programming languages as well.

  • We declare that we are going to create a new function with the keyword func.
  • We then set the name of the function, a set of parentheses to which we’ll specify the list of arguments that we want to pass to this function.
  • We then place our curly braces, and then inside of those curly braces is our function body.

Question :- How is the main.go file organised ?

Answer:- Well, in practice, it always ends up being the exact same pattern.

  • We’re always going to place our package declaration. So, remember we say, this file is a part of package, blah, blah, blah. In this case, package main.
  • Then right underneath that, we will list out all the other packages that we might need to import into this file. So, import statement for FMT and then maybe for IO or whatever other packages we want to get access to either from the standard library list of packages. We could also specify import statements for custom packages like reusable packages that, other team-mates might have authored.
  • After the package and import statements, we then get down to the body of the file, which is where we add in a bunch of logic that actually kind of does something. So it’ll be a collection of different functions, variable declarations and all that kind of other good stuff as well.

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next part of this series with Hands-On with Redis-Cluster.

References :-



aditya goel

Software Engineer for Big Data distributed systems