"Hello World" Application Using Golang



        Now the question is how to start writing a program in Golang? Is that very difficult to start programming in Go? The answer is No. Is that coding style is very different from using Golang? the answer is No. 
So now starts with the basic programming structure in Golang.

Basic Golang programming structure components:

1. Package Declaration
2. Import packages
3. Functions
4. Variables
5. Statements and expression
6. Comments

1. Package declaration:

As in other languages exp. java we need to define the package name first. So same in this language also we need to start with the declaration of the package name.

Every Go program starts with package  <packagename>

So for the package name string, there are only two values that can be possible, one is the main or the name of the directory it is in.

Package main tells to Go compiler for creating an executable file.

2.  Import packages:

To import required packages in our current writing program need to import that first.
We know why we need to import packages in the program I will not go into that fundamentals now. In short to use the data members from other packages we need to import that first. Following are the two ways to import package names.

 import "fmt"

 If we want to import multiple packages we can import like

import "fmt"
import  "strings"

The best way is

import (
"fmt"
"strings"
)

3. Functions:

To avoid repetition of code as other languages functions are there, Which is a step-by-step line of code that makes our program modular, structured, and reusable where it requires. Like functions, methods are there which we will learn in another part.

Syntax:  func FunctionName() {

// line of code

}

What is different in golang functions?

    1. Naming rules for the Go function

  • A function name must start with a letter
  • A function name can only contain alpha-numeric characters and underscores (A-z0-9, and _ )
  • Function names are case-sensitive
  • A function name cannot contain spaces.

    2. Multiple values can be possible to return from a function 
         exp.  

func calculateArea(length float64, width float64)(float64, error) { 
    
        // line of code   
 
        return area, error 
}        

    3. If consecutive parameters are of the same type, we can avoid writing the type each time and it is enough to be written once at the end. ie lenght float64, width float64 can be written as legth, width float64 

    4.  Named return values:
It is possible to return named values from a function. If a return value is named, it can be considered as being declared as a variable in the first line of the function.

exp: 

func calculateArea(length float64, width float64)(area float64, err error) {

        //line of code 

        return // here not need to return variable names if they are named already

          } 

    5.  Blank Identifier:

To discard accepting any response values from a function can use a blank identifier here. It can be used at the place of any value.

exp.  area, _ := rectProps(10.8, 5.6) // perimeter is discarded

In this example, we have discarded error value

4. Variables:

Variable is the name given to a memory location to store a value of a specific type.
There are multiple ways to declare variables in Golang.

    1. Declaring single variable
            var area float64

    2. Declaring multiple variables of the same type

            var area, perimeter float64 // we can declare multiple variables in a single statement

     3. Declaring a variable with an initial value 

            var area float64 = 12.5

     4. Type inference:-  If a variable has an initial value, Go will automatically be able to infer the type of that variable using that initial value.

            var side = 25 // Since the variable side has an initial value 25, Go can infer that it is of type int

     5. Short hand declaration:-  Golang provides another concise way to declare variables. This is known as shorthand declaration and it uses:= an operator
 
           side:= 25

 

5.  Statements and expression

    Like other languages, Go has also support for operators, conditional statements, and expressions.

  • Arithmetic operators
  • Relational Operators
  • Logical operators
  • Bitwise operators
  • Assignment operators

  Decision-making statements

  • if... statements
  • if...else statement
  • nested if statement
  • switch statement

Looping statements

  • for loops
  • nested loops

Loop control statements

  •  break statement
  • continue statement
  • goto statement

6. Comments:

 This is also very simple as other components.

 // single line comment

To block comments there are two  ways,

The first is by using a set of double forward slashes and repeating them for every line.

The second is to use opening tags (/*) and closing tags (*/).

/*
everything can comment
using this block
*/


Now by using basic fundamentals of language we will start with a simple program of "Hello World"

package main

import "fmt"
func main() {
// You can edit this code!
fmt.Println("Hello World")
}

Output: 

Hello World

Program exited.


Comments