This section will teach you a very interesting part of the course, Writing a program in Go. Before stretching any further, let us look at the program.
package main import "fmt" func main() { fmt.Println("Excited to learn Go!") }
In order to run this program and get the feeling of started to have learning a new language, you do not need to setup Go development environment on your machine, you can run it right now!!
Head over to Go playground, copy the above code, paste it there and click the Run button and see the output.
This might seem a very small program but contains important concepts that lay the foundation of learning Go.
Let’s dissect the program and understand every section.
This statement declares that the current program is in Go package whose name is main.
package
is a keyword which tells Go that the current file belongs to the package name following the keyword. package
statement should be the first statement in a Go file.
Every Go file should belong to some package unlike languages such as java and python which allow creating files without any package.
The name main
in the package declaration is significant since it tells Go that it is a package that will be executed.
In order to run a Go file, it should belong to the package main.
You will learn about package in detail in coming sections.
import “fmt”
An external package is imported into a Go file by using an import statement. Import statement is written using import
keyword followed by the name of package in double quotes.
Packages are imported so as to reuse the code written in them.
After importing a package, you can invoke the functions of that package using the name of package followed by a dot(.
) and the name of function along with required arguments such as that being done on Line 6 in the above example.
We will be covering this in detail in coming sections.
This statement is defining a function. Functions are resuable group of statements that can be invoked from different places in an application.
A function in Go is created using
func
keyword followed by the name of function and parenthesis(more on functions later).Name of the function can be a user defined name such as print, log, getName etc.
There should be one function with name main in a Go application and the file in which main function is declared should also belong to package main.
When a file is executed or run, this main function is automatically invoked by Go. In other words,
main function in package main is the entry point of a Go application.
If you do not provide main function in the file and try to run it, below will be the error
function main is undeclared in the main package
fmt.Println
Println
is a function that lies in package fmt. It accepts a string argument and prints it to the console.
In order to invoke this function, you need to import its package and then call it using a dot(.
).
Wrap up
To summarise, when the above example is executed, Go
- Looks that it is in package main,
- Imports the package
fmt
, - Searches for the main function,
- Executes the function.
- It invokes
Println
function from thefmt
package which in turn, prints the supplied string to the console.
In the coming sections, we will be looking at running Go programs on our local machine.