Getting Started with Go

Go is a statically typed, compiled programming language designed for simplicity, efficiency, and concurrency. Developed by Google, it aims to make software development straightforward and scalable.

Installing Go

“Ensure that Go is properly installed on your system. If it is not installed, you can refer to Chapter 1 to installing . You can verify the installation by opening a terminal or command prompt and typing:”

				
					go version

				
			

If installed correctly, it will display the installed Go version.

Setting Up Your Workspace

Create a directory for your Go projects. Inside this directory, you’ll typically have three  folder

  • src: Contains your Go source code files.
  • pkg: Stores package objects.
  • bin: Keeps executable binaries generated from your Go code.

Writing Your First Program

  • Let’s revisit the “Hello, World!” example and break it down further:

				
					package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

				
			
    • package main: Every Go file belongs to a package. The main package is the entry point for execution.
    • import "fmt": Imports the fmt package, which provides functions for formatting input and output.
    • func main() { ... }: Defines the main function, which serves as the entry point of the program.
    • fmt.Println("Hello, World!"): Calls the Println function from the fmt package to print “Hello, World!” to the console.

Here’s another example demonstrating a simple program in Go that calculates the sum of two numbers:

				
					package main

import "fmt"

func main() {
    // Define variables
    var num1, num2 int

    // Prompt user to enter two numbers
    fmt.Println("Enter the first number:")
    fmt.Scanln(&num1)
    
    fmt.Println("Enter the second number:")
    fmt.Scanln(&num2)

    // Calculate the sum
    sum := num1 + num2

    // Print the result
    fmt.Println("The sum is:", sum)
}

				
			
  • We import the fmt package to use its functions for input and output.
  • The main() function is the entry point of the program.
  • We define two integer variables num1 and num2 to store the user input.
  • We prompt the user to enter two numbers using fmt.Scanln() function, which reads user input from the console.
  • We calculate the sum of num1 and num2 and store it in the variable sum.
  • Finally, we print the result using fmt.Println().

In this chapter, we've explored the basics of Go programming, including installation, setting up your workspace, and writing your first program With this foundation, you're equipped to tackle more advanced topics in Go programming.Stay tuned for upcoming chapters where we'll delve deeper into Go's features and functionalities. Keep practicing and experimenting with code to reinforce your understanding. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India