Go Program Structure

In this tutorial you will learn about the Go Program Structure and its application with practical example.

Go Program Structure

Like any other programming language Go language have specification available for program structure, every Go program is consist of following building blocks –

  • Documentations block
  • Preprocessor Statements
  • The main ( ) function
  • Local Variable Declarations
  • Program statements
  • User defined functions

Example:-

Documentations Block

Documentation block is the header of any Go program structure which is mainly consist of set of comments, that is used to provide the information about the program written like name of a program, utility of program, date of creation, date of last modification ,author name, licensing or copyrights information and any other information that programmer wish to put for the references.

Example:-

Package Declaration

Here we define the package name in which this program would included. Go program runs in packages, so it is required to define a package. Every package has a name and path associated with it.

Preprocessor Statements

This is the section where we define all the pre-processor statements. The “import” pre-processor statements tells the compiler to import required packages prior to compilation of any Go program.

Recommended:-  Go Loops

The main ( ) function

This is the most vital part of each and every Go program, it is mandatory for Go program to have main ( ) function. The Go program execution starts with main ( ) function. Go program can not be executed without the main function.

Local Variable Declarations

In this section we declare all the variable that will be used in main ( ) function.

Statements and Expressions

This is the section where we place our main logic of the program which included the executable statements, that tell the computer to perform a specification action.Program statement can be an input-output statements, arithmetic statements, control statements, simple assignment statements and any other statements and it also includes comments that are enclosed within /* and */ .

User defined functions

This is where we put all the user defined sub-program or custom functions created to perform a specific task and called inside the main ( ) function.

In this tutorial we have learn about the Go Program Structure and its application with practical example. I hope you will like this tutorial.