Follow

Follow

Go Language Fundamentals: Variables, Data Types, Control Structures, Functions, and Error Handling

Maheshwar Ligade's photo
Maheshwar Ligade
·Apr 10, 2022·

4 min read

Play this article

Table of contents

  • Variables
  • Data Types
  • Control Structures
  • Functions
  • Error Handling
  • Conclusion

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

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.

Data Types

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}

Control Structures

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

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

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

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

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

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

Conclusion

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://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this