codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

1 / 72 topics
1Introduction to Go (Golang)2Installation and Setup3Hello World Program4Variables and Constants5Data Types6Operators7Control Structures (if, else, switch)8Functions9Defer Statement10Panic and Recover
Tutorials/Go (Golang)/Introduction to Go (Golang)
🐹Go (Golang)

Introduction to Go (Golang)

Updated 2026-04-20
3 min read

Introduction to Go (Golang)

Overview

Go, also known as Golang, is an open-source programming language developed by Google. It was first released in 2009 and has since gained popularity for its simplicity, efficiency, and strong support for concurrent programming. This tutorial will introduce you to the basics of Go, including its syntax, data types, control structures, functions, and more.

Setting Up Your Environment

Before we dive into coding, ensure that you have Go installed on your system. You can download it from the official Go website. Follow the installation instructions for your operating system to set up Go.

Once installed, verify the installation by running:

go version

This command should display the version of Go that you have installed.

Hello World

Let's start with a simple "Hello World" program. Create a file named hello.go and add the following code:

package main

import "fmt"

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

Explanation

  • Package Declaration: Every Go program starts with a package declaration. The main package is special because it defines a standalone executable program.

  • Import Statement: The import "fmt" statement imports the fmt package, which contains functions for formatted I/O operations.

  • Main Function: The func main() function is the entry point of any Go program. When you run the program, this function will be executed.

Running the Program

To compile and run your program, use the following commands:

go build hello.go
./hello

This will output:

Hello, World!

Basic Syntax

Go has a clean and simple syntax that is similar to C and Java. Here are some basic elements of Go syntax.

Variables

Variables in Go can be declared using the var keyword or the short variable declaration operator :=.

package main

import "fmt"

func main() {
    var name string = "Alice"
    age := 30

    fmt.Println("Name:", name)
    fmt.Println("Age:", age)
}

Data Types

Go is a statically typed language, meaning that the type of a variable is known at compile time. Here are some common data types:

  • Integers: int, int8, int16, int32, int64
  • Floating Point Numbers: float32, float64
  • Strings: string
  • Booleans: bool

Constants

Constants are declared using the const keyword.

package main

import "fmt"

func main() {
    const Pi = 3.14159
    fmt.Println("Value of Pi:", Pi)
}

Control Structures

Go supports standard control structures like if-else, for loops, and switch statements.

If-Else Statement

package main

import "fmt"

func main() {
    age := 20

    if age >= 18 {
        fmt.Println("You are an adult.")
    } else {
        fmt.Println("You are a minor.")
    }
}

For Loop

Go has only one loop construct, the for loop. It can be used in various ways.

package main

import "fmt"

func main() {
    // Traditional for loop
    for i := 0; i < 5; i++ {
        fmt.Println(i)
    }

    // While-like loop
    j := 0
    for j < 5 {
        fmt.Println(j)
        j++
    }

    // Infinite loop
    k := 0
    for {
        if k >= 5 {
            break
        }
        fmt.Println(k)
        k++
    }
}

Switch Statement

package main

import "fmt"

func main() {
    day := "Monday"

    switch day {
    case "Monday":
        fmt.Println("It's Monday.")
    case "Tuesday":
        fmt.Println("It's Tuesday.")
    default:
        fmt.Println("It's another day.")
    }
}

Functions

Functions are first-class citizens in Go. They can be passed around and returned from other functions.

Defining a Function

package main

import "fmt"

func add(a int, b int) int {
    return a + b
}

func main() {
    result := add(3, 4)
    fmt.Println("Result:", result)
}

Multiple Return Values

Go functions can return multiple values.

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x
}

func main() {
    a, b := swap("Hello", "World")
    fmt.Println(a, b)
}

Conclusion

This tutorial has covered the basics of Go programming, including setting up your environment, writing a simple program, basic syntax, control structures, and functions. Go's simplicity and efficiency make it an excellent choice for building scalable and high-performance applications.

In the next section, we will delve deeper into more advanced topics such as concurrency, error handling, and working with packages. Stay tuned!


Next Installation and Setup

Recommended Gear

Installation and Setup