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 are

go build HelloWorld.go
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

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 the 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 the main The next line is a package main which is a package declaration. package declaration always declared in the 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 take input and execute, 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. The function is println() which is prints line to the console. This function takes a string input and outputs the string to the standard console.

With this post, You can write a Sample hello world program in go language. The official website for this language isĀ  GoLang

Similar Posts