Go Part 2: Basic Data Types and Variables in Go

GO PART 2: UNDERSTANDING BASIC DATA TYPES AND VARIABLES IN GO


Please Subscribe Youtube| Like Facebook | Follow Twitter

Basic Data Types and Variables in Go

Go is a modern programming language designed for building efficient and scalable software systems. In this article, we will discuss basic data types and variables in Go. Understanding data types and variables is crucial to working with Go as they are the building blocks of any program.

Data Types in Go

In Go, there are several built-in data types that are used to store different kinds of information. The following are the most commonly used data types in Go:

  1. Numeric Data Types: Numeric data types include integers, floats, and complex numbers. Integers are whole numbers, while floats are decimal numbers. Complex numbers are made up of a real and imaginary part.
  2. Boolean Data Type: Boolean data type can only take two values: true or false.
  3. String Data Type: String data type is used to store a sequence of characters.
  4. Array Data Type: Array data type is used to store a fixed-size sequence of elements of the same type.
  5. Slice Data Type: Slice data type is similar to an array, but the size of a slice can change dynamically.
  6. Map Data Type: Map data type is used to store key-value pairs.

Variables in Go

In Go, a variable is used to store a value. A variable is created when a value is assigned to it. Go variables are statically typed, which means their data types are declared when they are created and cannot be changed later.

Declaring Variables in Go

In Go, a variable is declared by specifying the data type and assigning a value to it. For example, the following code declares a variable named x of type int and assigns the value 10 to it:

var x int = 10

Go Variable Naming Convention

When naming variables in Go, there are a few rules to follow:

  1. A variable name must start with a letter or underscore character.
  2. A variable name can only contain alphanumeric characters and underscore character.
  3. Variable names are case-sensitive.

Here is an example of a variable declaration that follows these conventions:

var firstName string = "John"
var lastName string = "Doe"
var age int = 25

Go Output Function

Go has a built-in function called fmt.Println() that is used to display output to the console. Here is an example of using the fmt.Println() function to display the value of a variable:

var x int = 10
fmt.Println(x)

Output:

10

Example Code

package main

import "fmt"

func main() {
	// Numeric Data Types
	var i int = 10
	var f float32 = 3.14
	var c complex64 = 1 + 2i

	fmt.Println("Integer:", i)
	fmt.Println("Float:", f)
	fmt.Println("Complex:", c)

	// Boolean Data Type
	var b bool = true
	fmt.Println("Boolean:", b)

	// String Data Type
	var s string = "Hello, World!"
	fmt.Println("String:", s)

	// Array Data Type
	var a [3]int
	a[0] = 1
	a[1] = 2
	a[2] = 3
	fmt.Println("Array:", a)

	// Slice Data Type
	var sl []int = []int{1, 2, 3, 4, 5}
	fmt.Println("Slice:", sl)

	// Map Data Type
	var m map[string]int = map[string]int{
		"apple":  1,
		"banana": 2,
		"orange": 3,
	}
	fmt.Println("Map:", m)
}

Output:

Integer: 10
Float: 3.14
Complex: (1+2i)
Boolean: true
String: Hello, World!
Array: [1 2 3]
Slice: [1 2 3 4 5]
Map: map[apple:1 banana:2 orange:3]

In this example, we declare several variables with different data types. Then We have used the fmt.Println() function to display the values of these variables.

Now below program demonstrates the statically typed nature of Go variables, as we specify the data type when declaring the variable and cannot change it later. For example, if we try to assign a string value to an integer variable, the program will not compile:

var x int = "10" // Error: cannot use "10" (type string) as type int in assignment

Overall, understanding data types and variables is an essential concept in Go programming. By learning how to use and manipulate different data types and variables, you can create more complex and powerful programs.

Go Beginner Tutorial Series

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *