[New post] 635

10 views
Skip to first unread message

Encapsulate {Ideas}

unread,
Aug 16, 2015, 5:51:10 AM8/16/15
to startc...@gmail.com
Er Utkarsh Bajpai posted: "First Program A Go program basically consists of the following parts: Package Declaration Import Packages Functions Variables Statements & Expressions Comments Let us look at a simple code that would print the words "Hello"
Respond to this post by replying above this line

New post on Encapsulate {Ideas}

635

by Er Utkarsh Bajpai

First Program

A Go program basically consists of the following parts:

  • Package Declaration
  • Import Packages
  • Functions
  • Variables
  • Statements & Expressions
  • Comments

Let us look at a simple code that would print the words "Hello World":

package main

import "fmt"

func main() {
   /* This is my first sample program. */
   fmt.Println("Hello, World!")
}

Let us look various parts of the above program:

  1. The first line of the program package main defines the package name in which this program should lie. It is a must statement as Go programs runs in packages. main package is the starting point to run the program. Each package has a path and name associated with it.
  2. The next lineimport "fmt" is a preprocessor command which tell the Go compiler to include files lying in package fmt.
  3. The next line func main() is the main function where program execution begins.
  4. The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program. Comments are also represented using // similar to Java or C++ comments.
  5. The next line fmt.Println(...) is another function available in Go which causes the message "Hello, World!" to be displayed on the screen. Here fmt package has exported Println method which is used to display message on the screen.
  6. Notice the capital P of Println method. In Go language, a name is exported if it starts with capital letter. Exported means that a function or variable/constant is accessible to importer of the respective package.

Execute Go Program:

Lets look at how to save the source code in a file, and how to compile and run it. Following are the simple steps:

  1. Open a text editor and add the above-mentioned code.
  2. Save the file as hello.go
  3. Open a command prompt and go to the directory where you saved the file.
  4. Type go run hello.go and press enter to run your code.
  5. If there are no errors in your code then you will be able to see "Hello World" printed on the screen.
$ go run hello.go
Hello, World!

Make sure that go compiler is in your path and that you are running it in the directory containing source file hello.go.

Er Utkarsh Bajpai | August 16, 2015 at 9:37 am | Categories: GO First Program | URL: http://wp.me/p5JOdg-af

Comment    See all comments    Like

Unsubscribe to no longer receive posts from Encapsulate {Ideas}.
Change your email settings at Manage Subscriptions.

Trouble clicking? Copy and paste this URL into your browser:
https://encapsulateideas.wordpress.com/2015/08/16/635/

Thanks for flying with WordPress.com

Reply all
Reply to author
Forward
0 new messages