Golang Program Structure

Golang | Program Structure: In this tutorial, we are going to learn about the Go language's program structure, how to write a Go language program?
Submitted by IncludeHelp, on March 25, 2021

Parts of Golang program

Before writing a Golang program, we must aware of its program structure. A Golang program consists of the following parts –

  1. Package Declaration
  2. Import Packages
  3. Functions
  4. Variables
  5. Statements & Expressions
  6. Comments
  7. Whitespaces & Blank lines

1) Package Declaration

Every Golang program must start with a package declaration. Since Golang programs run in packages so it is a mandatory statement.

The main package is the first statement of any program and it must be there. Package main is used for making the package as an executable program, it tells the compiler that this program is not a library it will be compiled as an executable program.

Syntax:

package package_name

2) Import Packages

The import is a keyword and it is used to import the packages i.e., to include the code from other libraries. The fmt (format) package is used for input & output formatting.

Syntax:

import (
    "package_1"
    "package_2"
    ...
)

3) Functions

Functions are the set of statements wrapped within a block, these are the building blocks of a Golang program. The func keyword followed by the function name is used to declare a function. In Golang, main() is a special function because it gets called when a program is executed.

Syntax:

func function_name(argument list){
    //statements
}

main() function syntax:

func main(){
    //statements
}

4) Variables

Variables are the identifiers of the memory blocks which are used to store the values. In Golang, the var keyword is used to declare a variable or list of variables. We can also use := shorthand to declare variables. A var statement can be at the package or function level.

Syntax:

var variable_list

Syntax (:= shorthand):

variable_name := value

Note: Outside of a function, every statement starts with a keyword such as var, func, etc. So the shorthand method cannot be used outside of the function.

5) Statements & Expressions

A statement is a command that instructs the compiler to take a specific action such as printing statements, conditional statements, looping statements, etc. And, an expression is the combination of the constants, variables, values, and operators and evaluates to a value.

Syntax (statement):

fmt.Println("Hello, world!")

Syntax (expression):

x = a+b;

6) Comments

Comments are the text or pieces of code that are written in the program and ignored by the compiler. It is used to write anything for the programmer or to explain any logic, statement, etc.

Comments in Golang start with a set of forwarded slashes (//) and continues to the end of the line. Multiple line comments can be written within the /* ... */ symbol.

Comments can be written anywhere in the program (inside the function, outside the function, before or after the program).

Syntax:

// This is a comment

/* This is also a comment, but
It's a multiple line comment*/

7) Whitespaces & Blank lines

Single (or multiple whitespaces) and blank lines can be used to format the Golang source code to beautify the code before the statement.

To understand a Golang program structure better – we are writing a simple "Hello world" program and explaining the parts which is used in it.

A simple Hello world program

// Golang program to print "Hello World".

package main

import "fmt"

func main() {
	fmt.Println("Hello world")
}

Output:

Hello world

In the above program,

  • The first line is the comment.
  • The second line is the blank line.
  • The third line is the Package Declaration and here, we are declaring the package main which tells the compiler that this program is an executable program, not a shared library.
  • Next line is the blank line again.
  • Then, there is import "fmt"; statement, which is the Import Package part of the program. Here, we are importing the fmt package for allowing the formatting-related functions.
    This import package statement can also be written like,
    import (
        "fmt"
    )
    
  • Then, there is another blank line.
  • After that, the functions block with a main() function is there. The main() function print (fmt.Println()) statement.

Executing a Golang program from the terminal/prompt

Let's discuss about the writing & executing a Golang program. To write a Golang program, compile it and execute the program. These are the steps,

  • Open an editor (text editor), write the code.
  • Save the file with the ".go" extension ("exhello.go")
  • Open the command prompt and go to the directory where the file is saved.
  • Then write the following command to execute the program
    $ go run exhello.go
    
  • The output will be printed on the screen.

Executing multiple Golang programs

If there are multiple Golang programs and we want to run them at once, we can either provide the file names separated by the spaces or use the glob pattern.

$ go run file1.go file2.go file3.go and so on...
$ go run directory_name/*.go 

Creating a binary executable file

If we want to create a binary executable file, we can use the following command,

$ go built exhello.go 

This command will create a binary executable file named "exhello" and it can be executed from the terminal/prompt like this,

$ ./exhello




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.