Go Tutorial Index

Every programming language follows a specific structure and approach to writing instructions. It is similar in the Go language as well. Before writing a program in Go, you must know its program structure. The entire program structure uses different types of tokens. This tutorial will guide you on how to write a simple Go program structure.

What Is Meant by the Structure of Programming Language?

The structure of a programming language defines how and in what order a programmer should write programs. Since we are learning the Go language, it usually consists of the following components:

A Go program consists of the following sections:
  1. Package Declaration
  2. Import Packages
  3. Functions Declaration and Definition
  4. Variables Declaration and Initialization
  5. Statements and Expressions
  6. Comments (if & when required)

Let's start with a simple GO program code.

Sample Code of the Go "Hello World" Program

Example:

/* Author: www.w3schools.in
Date: 2022-11-10
Description:
Writes the text "Hello, World!" on the screen */

package main

import "fmt"

func main() {
    var str = "Program."
    fmt.Println("Hello World!", str)
}

Program Output:

Hello World! Program.

The above Go example prints the text "Hello World! Program" to the screen. Let's explain what the above program segments do:

/* Comments */ The Go compiler ignores any statement written within /* ... */. These comments help write documentation and detailed descriptions of what a program, a particular set of statements, or a function allows us to do.
package main The first line determines the package name. Each package in a Go program has a path and name associated with it. This Go statement is mandatory because all Go programs run in packages. The main package is the initial point from which the program starts running.
import "fmt" The import statement is a preprocessor command statement that instructs the Go compiler to include files in the built-in package"fmt".
func main() The following line is func main(). This is where the compiler starts executing the code. All program execution begins from here.
Braces The { and } marks the start and end of the func main(). All the statements written within it come under the body of the main() function.
var str = "Program." In Go programming, we use var with the identifier name to declare variables, followed by the data type.
fmt.Println("Hello World!", str) Various built-in functions come bundled with built-in Go packages. The Println() is one such built-in method available within the fmt package of the Go language. It helps display the message passed within it as a string parameter; here, "Hello World! Program" gets displayed on the screen when this statement is executed. Here fmt package has exported the Println method used for displaying the message on the console output.

Executing a Go Program From the Terminal/Prompt

Once you are familiar with the basic structure of a Go program, you need to execute the program. The steps you should follow to execute a Go program are as follows:

  1. Open your editor and write or copy-paste the mentioned code.
  2. Save the program with a name and a .go file extension (in my case, "filename.go")
  3. Open a command prompt.
  4. Navigate to the directory where you saved the file.
  5. On the command prompt itself, type: go run filename.go and press enter to execute your code.
  6. If there are no errors in your code, you will probably see the program's output on your monitor screen.

Example:

D:\>go run filename.go
Hello World! Program.