Wednesday, November 7, 2018

Learn Golang Tutorials - Hello World First Program example Explained

In this blog post, We are going to learn the basics of Go Language tutorial with Hello World Example.
Go Language Tutorials - First Program code

Go Language Tutorials

Go is a popular Opensource programming language released by Google. When you are learning any language, You will write a sample Hello world example.
You need the following things to write and execute the Go Program
  • Go Compiler installation
  • use any code editor like Sublimetext or Atom, Visual studio code, and write code manually like below

Hello World First Sample Program Example

Like java programming language, To run the code, you need to do following things First you need to compile the code to generate machine-readable code which runs the code

// This is sample Hello world programming
package main
import "fmt"
func main() {
    fmt.Println("Hello World Sample First  Program")
}

The above two tasks can be done in many ways in this language
  • go run command 
  • go build command
Go compile and run the program First code needs to do the compilation. the compilation is the process of converting the code into low-level machine understandable code ie assembly code. We can compile the program in many ways using go run or go build commands go run command compiles the code and run the program
go run HelloWorld.go 

Hello World Sample First  Program
Other ways is
go build HelloWorld.go
dir
HelloWorld HelloWorld.go
This command compiles the code and generates an executable file. To run the program, We need to give executable via command line
/HelloWorld
Hello World Sample First  Program
We can use for compile and running the code using either go run or go build commands This program justs prints the string to the console.

Sample program Code explanation 

Here are the main components of the first program
  • Comments Declaration 
  • package keyword
  • import package 
  • Main Function Declaration
  • println function calling
Go code can be always read from Top to Bottom and left to bottom Below will walk through the code with explanations
Comments Declaration
Comments are used to tell the developer about the line of code or a file. These are ignored by Go Compiler Execution. Like java programming language, Comments are declared with starting // In the above program first line is declared with a comment. In Go comments can be declared in two ways for code Single Line comments can be declared as like below
// This is sample Hello world program
Multiple line comments can be declared like /* comment */
/*
* Line 1 Comment
* Line 2 Comment
*/
package keyword with main 

Next line is package main which is a package declaration. package declaration always declared in Go program. Packages are useful to group the code under a single name and can be reusable package main is required for standard libraries execution. The package declaration always the first line of code to be executed in Go Language. if the package is not declared in code, It gives compilation error like can't load package: a package main: expected 'package', found 'import'
can't load package: package main: 
prog.go:2:1: expected 'package', found 'import'
prog.go:3:2: expected ';', found "fmt"
import package 

Import keyword used to import other libraries packages into the current program. Here fmt standard package is imported so that all the classes and utilities can be used in the current code fmt package contains Input and output stream utilities.
Function Declaration 

Functions in any programming language are multiple statements executes in the sequential order and this can be grouped under one name. Functions takes input and executes, return output. Input can be either noting or any values and output can be nothing or any values. func is a keyword used to declare a function in golang. main is the name of the function which is always a starting point execution of go program code. functions are enclosed with braces{}. main() function always executes first when you run the code.
fmt.println() statement

Here fmt package's function is called. Function is println() which is print line to the console. This function takes string input and output the string to the standard console.

With this post, You are able to write a Sample hello world program in go language. Official website for this language is  GoLang

Related article


EmoticonEmoticon