Say Hello world in Go lang

Writing your first program "Hello World"

package main
import "fmt"
func main( ) {
       fmt.Print("Hello World")

Understanding the program line by line

  1. As we have discussed in the overview of go lang that Go program is organized in the various package. Here first statement package main specifies that this program will belong to the package main.
  2. Next line import "fmt" includes the predefined package fmt in your program so that you can use the functions (features) of this package in your program. Here import is the keyword in go lang use to import package(s).
  3. fmt stands for the format, fmt package has the various pre-defined function like Print, Println, Printf to print the formatted text and other methods like Scanf for reading the formatted values.
  4. Next Line defines the main() function, which is the starting point of every program. func is a keyword in go lang to define a function.
  5. Within the main() function we have written fmt.Print("Hello World"), which prints "Hello World" on the output screen.
  6. Observe the "P" in  Println(), It is written in Capital. This is a feature called "Exporting" in go language. Which means a Variable/ function is accessible outside the package if its name starts with Caps.
  7. In Go lang, a new line automatically ends the statement so fmt.Print() don't ends with semicolon(;) like in other programming languages C / C++ / Java.

Terminology used

  1. A package can be understood like a folder/directory in which you can manage multiple files, similarly, in go language, various related files are grouped together and put into a package for better management.
  2. In general, a function is anything which performs something or we can say a function is the group of statements together to perform a specific task.
  3. Multiple statements grouped together by using a block created by curly block{}.
  4. Print and Println both are pre-defined functions used to print the output but Println automatically changes the line after printing the enclosed output. So that the next output starts printing from the new line.


Popular posts from this blog

Program to calculate telephone bill

Arrays in C Language

Program to check a year is leap year or not.

Sum of two numbers in go lang

String in golang


Pointers in C Language

Data types in Java

Installing go lang

Decision Making in Go lang