Page Content

Tutorials

What Are The Steps To Create And Run Go Program, Structure

The general-purpose programming language Go, also referred to as Golang, was created at Google by Ken Thompson, Rob Pike, and Robert Griesemer. It was made public in November 2009. With the goals of efficiency, simplicity in programming, and speedy compilation, it was developed to tackle the difficulties associated with large-scale software development. Go places a strong emphasis on solid software engineering practices, simplicity, and a clear syntax.

Here are the Steps to Create and Run Go Program:

A “Hello, World!” program is typically the first program developed in any programming language. It only writes “Hello, World!” to the terminal.

Steps to Create and Run Go Program

Prerequisites: Installing the Go compiler and comprehending fundamental ideas like files, directories, and text editors are prerequisites for developing your first Go program.

Create a Folder: To save your program, create a new directory, such as ~/Go/src/golang-book/chapter2.

Write the Code: Type the following program into a text editor that has opened:

Save the File: Save the code as the main file.go to the folder you made. Text files ending in.go are commonly used to write Go applications.

Run the Program: Use the go run command to launch the program after opening your terminal or command prompt and navigating to the directory where you stored the file:

It should say “Hello World” to you. Because it creates a temporary executable from the files and launches the application without leaving any files on your hard drive, the go run command is useful. In order to create a statically linked executable binary for deployment, you would normally use go build.

Go Playground: You may edit, execute, and experiment with Go code right in your web browser with the Go Playground (play.golang.org) for rapid experimentation without local installation. Your code is compiled and run on Google servers.

Basic Syntax of a Go Program

A basic Go program is typically read top to bottom, left to right. It is structured with several key components, as demonstrated by the traditional “Hello, World!” program:

package main
import "fmt"
// This is a single-line comment
/* This is a
   block comment */
func main() {
    // A variable declaration with type inference
    message := "Hello, World!"
    fmt.Println(message) // Prints the message to the terminal
}

Output

Hello, World!

Basic Structure of a Go Program

From left to right, go programs are read from top to bottom. There are numerous essential components of a simple Go program:

Package Declaration (package main):

  • There must be a package declaration at the beginning of every Go program.
  • The way that Go organizes and reuses code is through packages.
  • The program’s executability the ability to run it straight from the terminal is indicated by the package main declaration. The Go compiler will be unable to determine where to launch the program if there is no main package.
  • A distinct package name is declared for libraries, which are collections of code for use in other applications.

Import Packages (import "fmt"):

  • Code from other packages may be included into your application using the import keyword.
  • A common library package that offers functions for formatted input and output, including printing text to the terminal, is called the fmt package (short for “format”).
  • The package name and a dot (fmt.Println, for example) are used to prefix functions from imported packages.
  • It is seen to be a cleaner style to import numerous packages by surrounding them in brackets.

Comments (// and /* */):

  • Comments are supplied for documentation and human readability, but the Go compiler ignores them.
  • Go is compatible with two styles:
    • //: comments that are one line long and contain all of the text that appears between // and the end of the line. When writing code documentation, this style is idiomatic.
    • /* */: Anything that falls between /* and */ is considered a block comment and may take up many lines. Blocks of code are frequently commented out using this technique for debugging.

Function Declaration (func main() { ... }):

  • The foundation of a Go program consists of functions.
  • The func keyword appears first, then the function’s name (for example, main), a list of zero or more arguments (inputs) contained in parenthesis, an optional return type (outputs), and a “body” encapsulated in curly brackets {}.
  • Since the main function is the first thing that is called when the program is executed, it is unique. The main package must include precisely one main() function that accepts no inputs and returns no values in order for a Go program to be executable.
  • The location of curly braces in Go is quite tight; the initial brace ` must be on the same line as the statement to which it belongs.

Statements and Expressions (fmt.Println("Hello World")):

  • A function’s body comprises a sequence of statements that are performed sequentially.
  • A declaration such as fmt.In Println(“Hello World”), a function (Println) from a package (fmt) is called, and an argument (the text “Hello World”) is sent.
  • In order to comprehend the code you write, Go depends on punctuation (quotes, brackets and braces). For example, quotes are not included in the output but indicate the start and finish of a string.

Other Fundamental Concepts Introduced Early

Data Types: Go uses statically typed characters. Basic data types such as float64 (for floating-point numbers), int (for whole integers), bool (for true/false values), and string (for text) are introduced in early programs.

Variables: Variables are used to hold data that may change during the course of a program. The Go compiler can determine the type depending on the given value whether they are defined using the var keyword with a type, or more frequently, using the abbreviated declaration :=.

Constants: Constants, which are defined with the const keyword, are comparable to variables but cannot be changed once they are declared. They work well for values that don’t change.

Operators: Go has common mathematical operators such as +, -, *, /, and %.

Whitespace: Programs are made simpler to understand by using whitespace (blanks, tabs, and newlines), which the compiler ignores. Go fmt, an automated formatting tool, is another feature that helps standardize code layout.

The simplicity, readability, and clarity of Go’s design make it an approachable language for novices.

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index