Web Technologies - Go Language

Back to Course

Lesson Description

Lession - #350 Go-Program Structure

Go - Program Structure

Before we concentrate on the essential structure squares of Go programming language, let us initially examine the absolute minimum construction of Go projects so we can accept it as a kind of perspective in resulting sections.

Hello World Example

A Go program fundamentally comprises of the accompanying parts −
  • Bundle Declaration
  • Import Packages
  • Capacities
  • Factors
  • Explanations and Expressions
  • Remarks
Allow us to take a gander at a basic code that would print the words "Hi World" −

package main

import "fmt"

func main(>
{ /* This is my first sample program. */ fmt.Println("Hello, World!">
Allow us to investigate the different pieces of the above program −
  • The principal line of the program bundle primary characterizes the bundle name in which this program ought to lie. It is an obligatory assertion, as Go projects run in bundles. The fundamental bundle is the beginning stage to run the program. Each bundle has a way and name related with it.
  • The following line import "fmt" is a preprocessor order which advises the Go compiler to incorporate the documents lying in the bundle fmt.
  • The following line func principal(>
    is the fundamental capacity where the program execution starts.
  • The following line/*...*/is disregarded by the compiler and it is there to add remarks in the program. Remarks are likewise addressed utilizing//like Java or C++ remarks.
  • The following line fmt.Println(...>
    is another capacity accessible in Go which causes the message "Hi, World!" to be shown on the screen. Here fmt bundle has traded Println technique which is utilized to show the message on the screen.
  • Notice the capital P of Println strategy. In Go language, a name is sent out on the off chance that it begins with capital letter. Traded implies the capacity or variable/steady is open to the shipper of the particular bundle.

Executing a Go Program

Allow us to examine how to save the source code in a document, arrange it, lastly execute the program. If it's not too much trouble, follow the means given beneath −
  • Open a content tool and add the previously mentioned code.
  • Save the document as hello.go
  • Open the order brief.
  • Go to the catalog where you saved the document.
  • Type go run hello.go and press enter to run your code.
  • On the off chance that there are no mistakes in your code, you will see "Hi World!" imprinted on the screen.

$ go run hello.go
Hello, World!

Ensure the Go compiler is in your way and that you are running it in the registry containing the source record hello.go.