Unlocking GoLang's Datatypes: A Statically Typed Journey into Integers, Strings, and Booleans
MetricsViews Private Limited
Building A Solid DevOps Strategy With Cloud Native.
In this article, we will see the Datatypes of Golang. Golang has a statically typed system with built-in data types to represent different states of values. It includes integer types, strings, booleans, and derived datatypes such as arrays, slices, maps, structs, pointers, and interfaces. Declaration of these datatypes is simple.
How it is statically typed:
In Go, being statically typed means that it knows what the types of your values are even before your program runs. The compiler checks and fulfills the type of correctness before the program runs. Functions in Go expect specific types for their parameters, and the return values also have predefined types.
In short, Go is statically typed. If you use the wrong type of value for assigning any value, Go will let you know. It will suggest that you are using the wrong data type.
?
Datatype:-
Data type is an important concept in Go like any other programming. Data type specifies the size and type of variable values.
?
There are three basic data types:
1.Numeric Datatype :- It represents integer values, floating point values, and complex types.
2.String Datatype :- It represents a string value.
3.Boolean Datatype :- It represents a boolean value that is either true or false. lets see these datatypes in detail:-
1.Numeric Datatype
·?Integer: Integer data types are used to store a whole numbers like 10, -20.
The integer data type has two categories:
(i)? Signed integers - It can store both positive and negative values like 50 and -50 also. Signed integers are declared with " int " keyword. There are types and sizes of int accordingly like,
?
int :-??? stores 32 bits in 32 bit systems and 64 bit in 64 bit systems.
int8 :- stores 8 bits , Range -128 to 127
int16 :- stores 16 bits, Range -32768 to 32767
int32 :- stores 32 bits, Range -2147483648 to 2147483647
int64 :- stores 64 bits, Range -9223372036854775808 to 9223372036854775807
?
Different ways of declaring Integer and assigning value to them:-
?
1)? var age int = 20
2)?????? var age int??????????????? //declaring
age = 20 // assigning
?3) age := 20 //declaring and assigning in one
?
领英推荐
(ii)? Unsigned integers - It can only store positive values like 10, and 20. Unsigned integers are declared with the " uint " keyword. It stores types and sizes like int only but, in unsigned integers, we use uint instead of int. For example- uint:- stores 32 bits in 32-bit systems and 64 bits in 64-bit systems.
Declaration of an unsigned integer is just like a signed integer you just have to use uint instead of int. For ex:-
1)? var age uint = 25
?
1.Float datatype
Float data types are used to store positive and negative numbers with a decimal point, like 20.5, -2.3, or 3567.49687. The default type of float is float64. If you do not specify a type, the type will be float64 automatically. There are types and sizes of float accordingly like,
?
float32:- stores 32 bits, Range -3.402823e+38 to 3.402823e+38
float64:- stores 64 bits, Range -1.797693e+308 to 1.797693e+308
?
2.String Datatype
A string is a series of bytes that usually represent text characters. You can define strings directly within code using string literals. The words between double quotation marks that Go will treat as a string. For example - "Hello world".
?
3.Boolean Datatype
Boolean values can be one of only two values: true or false. They’re especially useful with loop statements, which cause sections of code to run only if a condition is true or false.
?
Implementation of these datatypes
?
Output :
Integer value is: 25
Unsigned Integer value is: 10 Float value is: 3.14
Boolean value is: true String value is: Hello, Go!
Conclusion:
In this article, we have seen the fundamental concept of datatypes in GoLang, and how it is statically typed in nature. GoLang provides built-in data types such as integers, float, strings, and booleans and the example shows how to implement these datatypes in a simple way. The core datatypes covered include numeric types (integers and floats), strings, and booleans. Numeric types, such as integers, come in signed and unsigned variants with specific size ranges. The article also covered string datatypes, which represent sequences of characters, and boolean datatypes, which represent true or false values.
Credit- Priyanka Divekar (Backend)
MetricsViews Pvt. Ltd.
MetricsViews specializes in building a solid DevOps strategy with cloud-native including AWS, GCP, Azure, Salesforce, and many more.? We excel in microservice adoption, CI/CD, Orchestration, and Provisioning of Infrastructure - with Smart DevOps tools like Terraform, and CloudFormation on the cloud.