Basic Introduction


What is Go?

Go, commonly known as Golang, is an open-source programming language created by Google. It is designed for simplicity, efficiency, and concurrency, making it suitable for modern software development. Go combines the performance of statically typed languages like C with the ease of dynamically typed languages like Python.


Key Features of Go:

  • Statically Typed: Variables must have a defined type at compile time.
  • Compiled Language: Go code is compiled into machine code, ensuring high performance.
  • Garbage Collection: Automatic memory management for better efficiency.
  • Concurrency Support: Built-in goroutines make parallel programming simple.
  • Cross-Platform: Compatible with various operating systems like Windows, macOS, and Linux.

Installing Go

Before writing Go programs, you need to install the Go compiler.

  • Download Go from the official website: https://go.dev/dl/
  • Follow the installation instructions for your OS.
  • Verify installation by running:
go version

This should display the installed Go version.


Writing a Simple Go Program

A simple Go program includes a main package, an import statement, and a main function.

Example: "Hello, World!" Program

package main  // Defines the main package

import "fmt"  // fmt package for displaying output
func main() {
    fmt.Println("Hello, World!") // Prints "Hello, World!" to the console
}

Explanation:

  • package main – Every Go program begins with a package declaration, and the main package is necessary for executable programs.
  • import "fmt" – The fmt package is imported to enable formatted input and output operations.
  • func main() – The main function acts as the entry point of the program.
  • fmt.Println("Hello, World!") - The Println function of fmt package is used for displays text on the console.

Compiling and Running a Go Program

  • Save the file as main.go.
  • Open the terminal or command prompt and navigate to the file location.
  • Compile and execute using:
  • To build an executable file, use:
      go run main.go

      This directly runs the program without generating an executable file.

  • To build an executable file, use:
      go build main.go

      This creates a binary file (main.exe on Windows or main on Linux/macOS), which can be executed independently.


Conclusion

Go features a straightforward syntax and robust capabilities, making it a great option for system programming, web development, and cloud computing. Its speed, efficiency, and concurrency support make it stand out among modern programming languages.

Next