Variables And Data Types In Go

Hi, I am Vivek, a 26 years old developer from India. I love making apps. You can check my github for some of my open source projects.
Search for a command to run...

Hi, I am Vivek, a 26 years old developer from India. I love making apps. You can check my github for some of my open source projects.
In this series, I'll be posting articles to get you started with Google's GoLang.
Let's say we are working on a go app and we have to store the data of 2 users, so far we know about variables and how they can be used for data, let's say we create two variables and store the name of our 2 users. But then we receive a request to add...
Migrating a production database is often a high-stakes operation. Traditional "dump and restore" methods require significant maintenance windows, leading to service downtime. To solve this, PostgreSQL offers Logical Replication, a native mechanism th...

Ever found yourself in the endless cycle of "delete Docker image, redeploy, repeat" to keep your Dokku apps updated? You’re not alone. If your Dokku-deployed application relies directly on a Docker image (perhaps from Docker Hub or your private regis...

Lately, my internet has been throwing tantrums — not because the connection itself is bad, but because every time the power blinks, my router decides it's time for a little nap. Naturally, the obvious

So, one day I saw my old laptop sitting there, collecting dust like it was auditioning for a role in an apocalypse movie. And then it hit me: "Why not turn it into a server?" There was just one minor hiccup. Its Ethernet port was as dead as my dreams...

Recently I was tasked with setting up kubernetes cluster with an Nginx Ingress. As a newcomer to Kubernetes, I tackled this setup and learned a thing or two along the way. Let me walk you through my experience and share some helpful tips. The Goal: S...

Variables are containers that hold some value. There are two types of variables in Go, one whose value can be changed and the other whose can not we call them constants.
We can define a variable using the var keyword followed by the variable name and then its data type (what type value it is expected to store). Here are some examples:
var name string = "Go Language"
var number int = 23
var isTrue bool = true
fmt.Println(name)
fmt.Println(int)
fmt.Println(isTrue)
Using just
var name stringand not setting it to any value, it is called variable definition, we are defining what a variable's name is and what kind of data it can store and if it can be reassigned (set to a new value) or not.When we assign a value to a variable for the first time it is called variable initialization.
Constants are defined the same way as variables instead of using var we use const.
const name = "Vivek"
name = "Harsh" // Error: cannot assign to name
Go is statically typed, meaning, we need to tell the compiler about what type of data we are expecting.
We have multiple data types for different purposes. Here are some of them:
For textual data, we use the string type. We've been using the string data type from the start.
Anything within a double quote is considered a string. Here's an example:
var text string = "Hello"
Integer represents whole numbers positive or negative, like 5, -5, 0.
var positiveInt int = 5
var negativeInt int = -5
var zero int = 0
Signed integers, declared with int can store both positive and negative whole numbers. But there is a limit to how big of a number they can store.
| Type | Size | Range |
| int | 32 or 64 bits depending on 32 or 64 bit system | See the respective column |
| int8 | 8 bits | -128 to 127 |
| int16 | 16 bits | -32768 to 32767 |
| int32 | 32 bits | -2147483648 to 2147483647 |
| int64 | 64 bits | -9223372036854775808 to 9223372036854775807 |
Note: There are also
Unsignedtypes for integers.
Unsigned integers are declared with the uint keyword, these can only store positive whole numbers (yes this includes zero).
Here are the different variations of uint and their limitations:
| Type | Size | Range |
| uint | 32 or 64 bits depending on 32 or 64 bit system | See the respective column |
| uint8 | 8 bits | 0 to 255 |
| uint16 | 16 bits | 0 to 65535 |
| uint32 | 32 bits | 0 to 4294967295 |
| uint64 | 64 bits | 0 to 18446744073709551615 |
The float data types can store positive or negative decimal numbers:
| Type | Size | Range |
| float32 | 32 bits | -3.4e+38 to 3.4e+38. |
| float64 | 64 bits | -1.7e+308 to +1.7e+308. |
Here is an example of how we can use floats:
package main
import ("fmt")
func main() {
var x float64 = 234.43
var y float32 = 3.4e+38
fmt.Printf("Type: %T, value: %v\n", x, x)
fmt.Printf("Type: %T, value: %v", y, y)
}
fmt.Printf("text") is another method from the "fmt" package it is used to format an insert text at some predefined placeholders. You can learn more about fmt here.
A variable with bool data type can store either true or false. the default value for a bool variable is false.
var isTrue bool = true
var isFalse bool = false
var defaultValue bool
fmt.Println("isTrue:", isTrue)
fmt.Println("isFalse:", isFalse)
fmt.Println("defaultValue:", defaultValue)
Note: We'll talk about the arrays and maps separately.