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:
- 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.
- Boolean Data Type: Boolean data type can only take two values: true or false.
- String Data Type: String data type is used to store a sequence of characters.
- Array Data Type: Array data type is used to store a fixed-size sequence of elements of the same type.
- Slice Data Type: Slice data type is similar to an array, but the size of a slice can change dynamically.
- 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:
- A variable name must start with a letter or underscore character.
- A variable name can only contain alphanumeric characters and underscore character.
- 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
- Go Part 1: Setup and Introduction
- GO PART 2: UNDERSTANDING BASIC DATA TYPES AND VARIABLES IN GO
- Go PART 3: OPERATORS AND EXPRESSIONS IN Go
- GO PART 4: CONTROL FLOW STATEMENTS IN GO
- Go PART 5: Functions In Go
- GO PART 6: ARRAYS AND SLICES IN GO
- Go Part 7: String Manipulation In Go
- Go Part 8: Struct Type in Go
- Go Part 9: Pointers in Go
- Go Part 10: Error Handling in Go