Create Your First Program In Go Language (Learn 'Go' - Part 4)

in #programming6 years ago

Now that you have set up the working environment on your system, let's create the first program in Go.

Open up your workspace folder and create another folder within the 'src' folder. For Instance, 'Go/src/first-program/Hello World'

Launch your text editor and write the following code,

 
 
 

package main

import "fmt"

// This is a comment

func main() {
fmt.Println ("Hello World!")
}

 
 
 

Save the file as helloWorld.go in the folder we just created, i.e., 'Go/src/first-program/Hello World'.

Open the command prompt or terminal and change directory to 'Go/src/first-program/Hello World'.

Run the command to launch your first program by typing the following,

go run helloWorld.go

Congrats! You will see 'Hello World!' in your command prompt which means the program is successfully compiled and run.

 
 
 

'go run' Command

'go run' command compiles the program and turns it into an executable file before running it.

 
 
 

Now, let's see what different components are there in your program.

 
 
 

Package Declaration

Every Go program begins with a package declaration. Packages are used to organize and reuse the code.

We have used the 'main' package which is prebuilt and reusable for any go program.

'package main'

 
 
 

WhiteSpace

Go compiler ignores the whitespaces, i.e., space, tabs, and newlines. We use them for better readability of the code.

 
 
 

Import

We use 'import' keyword to import code from the other packages. We have imported the "fmt" package for formatting. We need to use the "double quotes" while importing any packages.

 
 
 

Comments

We can use '//' for a single line comment or use /* comment */ for multiline comments. The compiler ignores comments but we use them for our own convenience and the future reference of the code.

 
 
 

Functions

Functions are the building blocks of a program and they contain statements that are executed sequentially.

We use 'func' keyword to start a function followed by the name of the function(main, in this case). This is our main function of the program and every Go program needs a main function to begin with. It is the first function which is called when we execute the program.

The paranthesis() are used after the name of the function and we can add parameters to our function with them. The body of the function is surrounded by the curly braces{}, which is optional.

 
 

func main(){
fmt.Println("Hello World!")
}

 
 

In this function, we don't have any parameters and the program basically executes just one line of code.

 
 

fmt.Println("Hello World!")

 
 

Inside the 'fmt' package, we call the 'Println' function and create a String "Hello World!" before executing the program.

The real work is done by the 'Println' function in our program and it displays the String on the screen.

 
 
 

All the programs follow the same basic structure but the complex programs use multiple libraries, packages, functions and a lot more to be useful in the real world. Just look at the code again and see the different components of the program in action.

 
 
 


 
 
 

Previous Posts In The Series

 
 

Introduction To 'Go' Programming Language(Learn 'Go' - Part 1)

 

25 Basic Keywords Of The Go Programming Language (Learn 'Go' - Part 2)

 

How To Set The Go Programming Environment On Your System?(Learn 'Go' - Part 3)


 
 

Upcoming Posts

 
 

Strings In 'Go'(Learn 'Go' - Part 5)

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.032
BTC 61227.60
ETH 3022.96
USDT 1.00
SBD 3.88