Write a simple program with Go
Compile a program
Specifications
Compilation
A binary / an executable
In this chapter, we will write our two first Go applications.
\n\nBefore writing any code, we need to decide what our application will do. Most projects begin with this phase. This is called the specification phase. It aims to produce precise requirements that the application should fulfill. Those requirements are the specifications (or the specs).
\nOur application specification is simple: when launched, the application should display the date and time, and exit.
\n\nA Go application is composed of files. On those files, we will write Go code. We call those file “source files”. An application is stored in the main directory. This main directory can contain just one source file. Most of the time, it’s composed of several sub-directories.
\nWe will create this main directory for our application. You can do it using the command line (or with the graphical interface of your system) :
\n$ cd Documents/code\n$ mkdir dateAndTime
\n\nWe are just minutes away from writing code. Do you need special software to write Go code? In theory, you can write code with a standard text editor. There is dedicated software on the market specially developed for developers. They are called IDE: Integrated Development Environment.
\nIDE provides functionalities like :
\nAutomatic coloration for reserved words (syntax highlighting)
Autocompletion
Refactoring capabilities
...
They are many IDE on the market. You can search google to find the best fit for you. I use Goland, which IntelliJ develops. This software is not free (it’s subscription-based), but I find it easy to use.
\n\nLet’s create our source file. We will name it main.go.
\n// first-go-application/first/main.go\npackage main\n\nimport (\n "fmt"\n "time"\n)\n\nfunc main() {\n now := time.Now()\n fmt.Println(now)\n}
\n\nThe first line is mandatory in Go. In all files, you must add the package declaration. Such declaration is composed of the keyword package then the name of the package.
On the second line, you can read another keyword: import. It is usually followed by an open parenthesis and a list of the program’s imported packages. Every package is written on a new line. Each package has a name delimited by double-quotes. Here our application depends on two packages :
\nfmt
time
Those packages are part of the standard library.
Then you find the declaration of a function named main. We will go deeper into the syntax of functions later.
\nThe function declaration is enclosed with curly brackets : { and }.
Inside the function declaration, we have two statements :
\nThe first instruction is an affectation. We initialize the variable “now” and we give it the value returned by the function call Now()from the package time.
The second instruction is a call of the function Print from the package fmt
WARNING! be sure that you only use packages that you actually import. Otherwise your program will not compile... When you use a package not imported, your Go program will not compile
Make sure that you use imported packages. In the following code sample I import the package fmt
and time
but I do not use them in my main
function :
// DO NOT COMPILE\n// first-go-application/import-issue/main.go\npackage main\n\nimport (\n "fmt"\n "time"\n)\n\nfunc main(){\n\n}
\n\nThe main function is the entry point of the program. In every application, you have at least one main function. The program will start with the first statement of this function. (Note that in C, C++, Java, the main function’s concept exists).
\n\nThe source file is ready to be transformed into a binary (or executable). To do so, we will use the Go toolchain. Open a terminal :
\n$ cd Documents/code/dateAndTime\n$ go build main.go
\nThe first instruction (cd) instructs the shell to change the current directory to Documents/code/dateAndTime. This command will compile the program into an executable. The executable is named main (the same name as the source file, without the .go extension). Let’s see the files that are now into our dateAndTime directory :
\n$ ls -lh\ntotal 4160\n-rwxr-xr-x 1 maximilienandile staff 2.0M Aug 16 11:27 main\n-rw-r--r-- 1 maximilienandile staff 94B Aug 16 11:00 main.go
\nWe use the command ls. (for windows user, you can use the command dir). You can see that we have two files :
\nmain that weights 2.0M (MegaBytes), and that is executable.
main.go that weights only 94 Bytes. (this is our source file).
Now it’s time to launch our application :
\n$ ./main\n2019-08-16 11:45:44.435637 +0200 CEST m=+0.000263533
\nCongratulations on your first Go application!
\n\nHow to compile a Go application?
How is the result of the compilation called?
What is the name of the entry point function of a Go application?
What is the usage of the import statement?
How to compile a Go application?
\nOpen a terminal
Go to the directory of your application. Providing that there is a file named main.go that contains a main function issue those commands :
\n$ cd /code/myApp
$ go build main.go
How is the result of the compilation called?
\nWhat is the name of the entry point function of a Go application?
\nWhat is the usage of the import statement?
\nIt’s used to import packages (from the standard library or other sources) into the program.
An imported package can then be used inside the code.
Create a go application that displays the string “Hello World” on the screen and exit.
\n// first-go-application/hello-world/main.go\npackage main\n\nimport "fmt"\n\nfunc main() {\n fmt.Println("Hello World")\n}
\nNotes :
\nHere you can see that we use just one dependency. The import part of the program has changed; parenthesis is not required when importing only one package.
We have a main function. And a single statement inside.
Here the function Println is called. This function is part of the package fmt (fmt stands for “formatting”).
The package fmt is part of the standard library.
To create a simple program
\nCreate a file
Name it main.go
Here is the basic skeleton of a program (that does nothing)
// first-go-application/skeleton/main.go\npackage main\n\nfunc main() {\n \n}
\nThis file is denoted “source file”
From this source file, we can create an executable program (that can be launched).
The creation of the executable is called “compilation”.
To compile a program, type the following command into the terminal
\nTo launch the compiled program, type the following into your terminal :
\nPrevious
\n\t\t\t\t\t\t\t\t\tSetup your dev environment
\n\t\t\t\t\t\t\t\tNext
\n\t\t\t\t\t\t\t\t\tBinary and Decimal
\n\t\t\t\t\t\t\t\t