First Program

This section will teach you a very interesting part of the course, Writing a program in Go. Before stretching any further, let us look at the program.

package main

import "fmt"

func main() {
   fmt.Println("Excited to learn Go!")   
}

In order to run this program and get the feeling of started to have learning a new language, you do not need to setup Go development environment on your machine, you can run it right now!!

Head over to Go playground, copy the above code, paste it there and click the Run button and see the output.
This might seem a very small program but contains important concepts that lay the foundation of learning Go.
Let’s dissect the program and understand every section.
[the_ad id=”651″] package main
This statement declares that the current program is in Go package whose name is main. package is a keyword which tells Go that the current file belongs to the package name following the keyword.

package statement should be the first statement in a Go file.

Every Go file should belong to some package unlike languages such as java and python which allow creating files without any package.
The name main in the package declaration is significant since it tells Go that it is a package that will be executed.
In order to run a Go file, it should belong to the package main.
You will learn about package in detail in coming sections.
import “fmt”
An external package is imported into a Go file by using an import statement. Import statement is written using import keyword followed by the name of package in double quotes.
Packages are imported so as to reuse the code written in them.
After importing a package, you can invoke the functions of that package using the name of package followed by a dot(.) and the name of function along with required arguments such as that being done on Line 6 in the above example.
We will be covering this in detail in coming sections.
[the_ad id=”656″] func main
This statement is defining a function. Functions are resuable group of statements that can be invoked from different places in an application.
A function in Go is created using func keyword followed by the name of function and parenthesis(more on functions later).
Name of the function can be a user defined name such as print, log, getName etc.
There should be one function with name main in a Go application and the file in which main function is declared should also belong to package main.
When a file is executed or run, this main function is automatically invoked by Go. In other words,

main function in package main is the entry point of a Go application.

If you do not provide main function in the file and try to run it, below will be the error

function main is undeclared in the main package

fmt.Println
Println is a function that lies in package fmt. It accepts a string argument and prints it to the console.
In order to invoke this function, you need to import its package and then call it using a dot(.).
[the_ad id=”3385″] Wrap up
To summarise, when the above example is executed, Go

  1. Looks that it is in package main,
  2. Imports the package fmt,
  3. Searches for the main function,
  4. Executes the function.
  5. It invokes Println function from the fmt package which in turn, prints the supplied string to the console.

In the coming sections, we will be looking at running Go programs on our local machine.

First Program

Create a new folder anywhere on your machine. Open command prompt and navigate to the newly created folder. Type command code . (code space dot) which will open VS Code into this folder. You can use any editor such as Atom, sublime etc., or a text editor such as notepad also. It is not necessary to use VS Code.
[the_ad id=”89″] Example, create a folder named node anywhere on your machine. Open VS Code in this folder and click new file icon as shown in the image below.
first node program

For opening VS Code in a folder, navigate to the folder location in command prompt and type code . and hit enter.

Name the new file as firstprogram.js. Note that the file ends with a .js extension. Reason is that node comprises of a javascript engine and hence it only understands js files.
In the newly created file, write the below code.

function sayHello() {
    console.log('Learning Node.js!!!');
}

// call function
sayHello();

[the_ad id=”94″] Above code creates a simple javascript function and calls it.
Now in the command prompt which is open at the location of the above file, type node firstprogram.js
You will see an output as below.

Learning Node.js!!!

Note that all the code written in node is in javascript, so you need to be familiar with javascript for learning node. You can learn javascript here.
[the_ad id=”95″]

First Program

First Python Program
In this section, you will be writing a simple python program for printing a message as the program output. Python provides a function called print which takes a string and prints it to the console.
There are 2 different ways to write a python program.

1. Using Python Interactive shell
Open Command prompt, type python and hit enter. This will launch python interactive shell which shall look as below.

For printing a message, write print function with “Learning Python!” as its arguments and hit enter. This will print the message on terminal as shown below.


Congratulations!!! you have just executed your first python program from python shell.

[the_ad id=”89″]

2. Using Python program file
Open your favorite text editor and type

print("Learning Python!")

Save this file as program.py anywhere on your system.
Now open command prompt, navigate to the location where the above file was saved and type command

python program.py
and hit enter.
This will print “Learning Python!” on the terminal.
Congratulations!!! you have just executed your first python script.

Note: Python command will only work if the path of python of python installation is added to search path of your operating system such as path environment variable on Windows or PATH variable in Linux.
[the_ad id=”94″]

3. Using an IDE
Open your favorite IDE and create a new project. On PyCharm, go to File -> New Project, give your project a name and hit Enter. Next right click of the project and select New -> Python File. Write the above print statement in that file.
Finally right click anywhere on the file and Select Run ‘program’. It will execute the program and display the output.

Exit mobile version