Go - Program Structure

Go - Program Structure : basic building blocks of Go programming language.

Hello World Example

The following list basic of Go :
  • Package Declaration
  • Import Packages
  • Functions
  • Variables
  • Statements and Expressions
  • Comments
Let us lookup basic code that would print the words "Hello World Welookups" −
package main

import "fmt"

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

Let us take a look at the various parts of the above program :

The first line of the program package main defines the package name in which this program should lie.

func main() is the main function where the program execution begins.

The next line /*...*/ is ignored by the compiler and it is there to add comments in the program.

Executing a Go Program

Let us discuss how to save the source code in a file, compile it, and finally execute the program.
  • Open a text editor and add the above-mentioned code.

  • Save the file as hello.go

  • Open the command prompt.

  • Go to the directory where you saved the file.

  • Type go run hello.go and press enter to run your code.

  • If there are no errors in your code, then you will see "Hello World!" printed on the screen.

$ go run hello.go
Hello, World Welookups!