Go Language Fundamentals: Variables, Data Types, Control Structures, Functions, and Error Handling
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
Go is a statically typed, compiled programming language designed for building efficient and scalable software. It offers a range of features and concepts that make it a popular choice for building robust applications. In this article, we will cover the core concepts and features of Go, including variables, data types, control structures, functions, and error handling.

Variables are used to store values that can be used throughout your program. In Go, variables are declared using the var keyword, followed by the variable name and its data type. Here's an example:
var age int = 30
In this example, we've declared a variable called age with a data type of int, and initialized it with a value of 30.
You can also declare and initialize a variable on the same line, as this:
goCopy codeage := 30
This is known as a short variable declaration.
Go has several built-in data types that you can use to store different kinds of values. Here are some of the most common data types in Go:
int - used to store integer values
float64 - used to store floating-point values
bool - used to store true/false values
string - used to store text
Go supports a range of data types, including integers, floating-point numbers, booleans, strings, arrays, slices, maps, and structs. Here are some examples:
// Integer
var age int = 30
// Floating-point number
var weight float64 = 68.5
// Boolean
var isStudent bool = true
// String
var name string = "John"
// Array
var numbers [3]int = [3]int{1, 2, 3}
// Slice
var colors []string = []string{"red", "green", "blue"}
// Map
var scores map[string]int = map[string]int{"John": 90, "Sarah": 85}
// Struct
type Person struct {
Name string
Age int
}
var person Person = Person{Name: "John", Age: 30}
Go provides a range of control structures that allow you to control the flow of your program. These include if/else statements, switch statements, loops, and more.
If/else statements are used to execute code conditionally. Here's an example:
if age >= 18 {
fmt.Println("You are an adult")
} else {
fmt.Println("You are a child")
}
In this example, we're using an if/else statement to check if the age variable is greater than or equal to 18. If it is, we print "You are an adult". Otherwise, we print "You are a child".
Switch statements are used to compare a value against a range of possible values. Here's an example:
switch grade {
case "A":
fmt.Println("Excellent")
case "B":
fmt.Println("Good")
case "C":
fmt.Println("Average")
default:
fmt.Println("Fail")
}
In this example, we're using a switch statement to check the value of the grade variable against the possible values of "A", "B", "C", and the default case, "Fail".
Loops are used to execute a block of code repeatedly. Go supports for loops and while loops.
Here's an example of a for loop:
for i := 0; i < 10; i++ {
fmt.Println(i)
}
In this example, we're using a for loop to print the values of i from 0 to 9.
Here's an example of a while loop:
for age < 18 {
fmt.Println("You are not an adult yet")
Functions are used to encapsulate a block of code that can be reused throughout your program. Here's the syntax for creating a function in Go:
func function_name(parameters) return_type {
// code to execute
}
For example, if you wanted to create a function called add that takes two integers and returns their sum, you would use the following code:
func add(a int, b int) int {
return a + b
}
Error handling is an important part of any programming language. In Go, errors are represented by the error type. Here's an example of how to handle errors in Go:
result, err := some_function()
if err != nil {
// handle the error
} else {
// use the result
}
In this example, some_function returns a result and an error. If the error is not nil
This article gives you a very brief overview of the basic fundamental building blocks of go language each of these topics has its bible but this is just to start with go language for better study refer to the individual topic blog post.
I hope this helps, you!!
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.