Anatomy of GoLang Program

go run <NAME_OF_FILE.go>
  • 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.
  • 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
  • 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.
  • 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.
  • Executable
  • ReUsable
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems