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:-
1 2 3 4 5 6 |
/* This is my first Go program. */ package main import "fmt" func main() { fmt.Println("Hello, World!") } |
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:-
1 2 3 4 5 6 7 |
/* Program Name: First Go Program Version: 1.0 Description: Go program basic program structure. Author: @W3Adda Date Created:21-06-2018 */ |
Package Declaration
1 |
package main |
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
1 |
import "fmt" |
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.
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.