First GoLang Program

First GoLang Program: Writing the program or source code is very easy in the go language. You need to open your text editor and open a new file. You can also open the new file by pressing the ctrl+N button after opening your text editor. Now write the source code in this new file.

Here the first program for you in the go language is given below.

Program of Hello World:

Here a program is given below in which you will how to print a string, i.e., "Hello World". The program is as follows:

 package main
 import "fmt"
 func main() {
             fmt.Println("Hello World..!! Welcome in the era of Go language")
             fmt.Println("Congratulations..!! Your first Golang Program run successfully.")
 } 

Here, your go program is complete. Type it the same in your file as it is given the upside. Now save the file with the .go extension.

Run the program:

Case 1:

You can run your go program on the terminal window or command prompt. Some text editors also provide the command prompt window; you can run your program by running the following command in your command prompt:

                        run FILE_NAME.go

Case 2:

If you are using a simple text editor like notepad, you can run your go program on your system's command prompt. Follow the given steps to run your go program easily:

Step 1:

First of all, open your command prompt.

It is easy to open the command prompt. Press the window+R key from your keyboard, and a pop-up box displays on the screen i. e. Run box. Type "cmd" inside the open bar and press Okay to proceed.

First GoLang Program

Step 1.1:

When you click on the okay button, then a command prompt window appears on the screen.

First GoLang Program

Step 2:

Now change the directory to that folder where your file is saved with the help of the "cd" command.

                                                cd PATH_OF_THE_SOURCEFILE
First GoLang Program

Step 3:

Now type the go command to run the go file. The command is:

go run FILE_NAME.go
First GoLang Program

Step 4:

Congratulations..!! Now your go file will be executed successfully. If there will be no error in your go source code, then your go program will run successfully and give the result; otherwise, it will display the respected error you made in your program.

Output:

First GoLang Program

Explanation of the Go Program:

package main: In the first line of every go program, there will always be a package declaration, which means every go program will start with the package's declaration. It is necessary to include the main package in your go program. Packages are used to organize the source code and provide the reusability of the program's codes. 

There are two types of programs available in the go language, which are:

  1. Executable
  2. Library

Import fmt: The second line of your go program >> is import “fmt"; the import is a keyword of the go language used to import the packages in your go program. Here it is importing the fmt package, also known as format package. The fmt package provides formatted input and output implementation with the functions in the go program.

Func: func is stood for function. Func is a keyword of the go language used to create or declare a function in the go program. 

Main(): Main() is the function of the go language. The execution process of the go program will start from here. This function is a non-return type function, which means it does not return any value in the program. It is also a non-parameterized function means no parameters will be declared with this function. The compiler runs the main function of the program firstly. It will call itself automatically when you execute the program. The entire go program starts from here. You can say that the main() is the body of your go program. All the codes will be written inside the main section.

Println(): Println stands for the print line. Println is a method that is present in the fmt package. This method is used in the go program to display the string. The Go language is a case sensitive language. The first letter of this method, i.e., 'P' always be in the upper case. 



ADVERTISEMENT
ADVERTISEMENT