Variables, Constants and Built-in Data Types in Go
Tags: go
Published: 2022-03-16 11:30:22

## **Variables in GO**

- Variable is the name given to a memory location to store a value of a specific type. 
- In Go variables can be declared in two ways.
- Static data type declaration; using var keyword e.g. var varname  datatype
- Dynamic data type declaration; using := operator e.g. varname := value
- The var keyword declaration can be used both inside and outside of function.
- The := operator declaration cannot be used inside function.
- In variables declared with := operator, the type of the variable is inferred from the value.
- If a variable is not assigned any value, Go automatically initializes it with the Zero value of the variable's type.
- In Go variables are case sensitive.
- In Go variable names are written in camelCase.
- Multiple variable declarations can be grouped into a block for greater readability e.g. var (a datatype1 c datatype2)

## Comments

- Comments are lines of text that are used to make code more readable.
- Comments are ignored by the compiler during the compilation.
- To write a single line comment; begin a new line in your code with two forward slashes //. 
- To write a multi-line comment; enclose comments in /* …. */

## Built-in Data Types in GO

- Data type specifies the type and size of values variable will hold.
- Go has three built-in data types.
- Numeric
- String
- Boolean
- To get the data type of any variable we can use the TypeOf() function from reflect package.
- To demonstrates the variable declaration in Go; create new folder as 02-01 Variables in src folder of the workspace and create var.go file and type the following code.

{% embed https://gist.github.com/ZammaCode/ae26fcdf13e18575a6676695647585cd.js %}

- Following is the output of above code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48v6djp45v82mrk0dorf.png)

- Finally commit changes to your repo and push to GitHub.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njzaj154tumj625xeara.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8t4sggxp7ro6xxh7yfd.png)

## Constants in GO

- Constants are variables whose values cannot be changed later.
- Constant variables are declared using const keyword instead of using var and its value must be assigned during declaration.
- Constant names are usually written in uppercase letters.
- Constants can be declared either as typed or untyped.
- To demonstrates the constants declaration in Go; create new folder as 02-02 Constants in src folder of the workspace and create constant.go file and type the following code.

{% embed https://gist.github.com/ZammaCode/261f3b2811c59e05c9c5d526c352e4e5.js %}

- Running the above code produce the following output.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0ktg2o16nt80qn0dn0i.png)

- Commit changes to GitHub.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8fc1zfcj2zjwg6bd4wx.png)

## Numeric Data Type

- Numeric types falls into whole numbers, decimal and complex numbers.
- Go offers support for five different sizes of signed and unsigned integers or whole numbers, named int, int8,  int16, int32, int64; and uint, uint8, uint16, uint32, and uint64.
- There are two types of decimal or float numbers i.e. float32 and float64.
- Go also supports representation of complex numbers by using complex64 and complex128   
- int32 and uint8 has also aliases namely rune and byte respectively. 
- Rune and byte are used to distinguish character values integer values (byte represents ASCII characters while rune represents Unicode character).
- The sizes of int and uint are implementation specific.
- To demonstrates the numeric data types and its variable declaration; create new folder as 02-03 NumericVariables in src folder of the workspace and create numeric_variables.go and type the following code.

{% embed https://gist.github.com/ZammaCode/2cfb19c92d9ef6fd9f43b5950817a562.js %}

- Following is the out of above mentioned code.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z23ly4jj8ukuudknwpju.png)

- Push code to GitHub.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2l2jk1v5n162947smmns.png)

## String Data Type

- Strings in Go are sequence of characters where each and every character is represented by one or more bytes using UTF-8 Encoding.
- Strings are sequence of bytes representing Unicode characters.
- String is a slice of bytes. Slice is just like an array having an index value and length, but the size of the slice is resized they are not in fixed-size just like an array.
- In Go single strings are surrounded by double quotation marks (“”) while multiline strings can be defined using the backtick marks (``).
- Strings are immutable which means once a string value is assigned to a variable, the value of that string is never changed.
- To demonstrates the string data types and its variables declaration; create new folder as 02-04 StringVariables in src folder of the workspace and create str_variables.go and type the following code. 

{% embed https://gist.github.com/ZammaCode/aa91a3ef46dae039f66a7fd10aa7990b.js %}

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l1dpbmxs0263tpttjatb.png)

- Commit your code to GitHub. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2rae2u279rj5jnae2kr.png)
 

## Operations on String Variables
**1. Accessing String Characters**
- String is a slice of bytes.
- In slice index always starts at zero.
- To access a specific character we use the index of an array in square brackets
- Create str_operations.go in 02-05 StringOperations folder and type the following code and run.

{% embed https://gist.github.com/ZammaCode/5bcadc9641ccf450b48322440ffe674a.js %}

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0934epthvwk168gp0wmq.png)

**2. Accessing Substring**
- The Split and SplitAfter methods of strings standard library returns the substring in the form of slices.
- Continue in the previous str_operations.go file, type and run the following code.

{% embed https://gist.github.com/ZammaCode/7c2f6d076d305556f23d30d2da51fd40.js %}

- Output of the above code snippet.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08wzyk9j80lxase5dnmu.png)

**3. Strings are Immutable**
- Once a string is created it's not possible to change it.
- Continue in the previous str_operations.go file, type and run the following code.

{% embed https://gist.github.com/ZammaCode/96b1ea80fef92cbd87914e0f0364ea43.js %} 

- On execution of the above code the following error will be raise by the compiler.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ftftw4mubwzmpormzml6.png)

**4. String Concatenation**
- To perform string concatenation we can use the + operator.
- Continue with the str_operations.go file, type and run the following code.

{% embed https://gist.github.com/ZammaCode/dbe24e1b98cc3cb7c2131005a600b54f.js %}

**5. String Comparison**
- The == operator is used to compare two strings for equality.
- Continue with the str_operations.go file, type and run the following code.

{% embed https://gist.github.com/ZammaCode/36f4b78883142ca407b9810a83afbd1f.js %}

**6. String Length**
- The len function returns the number of bytes in string while the RuneCountInString function of utf8 package can be used to find length of string.
- Continue with the str_operations.go file, type and run the following code.

{% embed https://gist.github.com/ZammaCode/6689ea21c6e7aa07d059890fe63bf0ee.js %}

- Following is the output.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4xx4bi27af5ucpsiisw.png)

- Open up Git Bash and Commit changes to GitHub.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ff51vkhmmz9ooyj20nso.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ak96uuihechymdwhv24j.png)

## Boolean Data Type
- A bool type represents the set of Boolean truth values denoted by the pre declared constants true and false.
- The default value of a boolean data type is false.
- To demonstrates the boolean data type and its variables declaration; create new folder as 02-06 BooleanVariables in src folder of the workspace and create bool_variables.go and type the following code.

{% embed https://gist.github.com/ZammaCode/2c7f86dfead81cded999225eca0fa1f8.js %}

- On execution the above code will print the following output.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9q19khb3slrz4hxas8yd.png)

- Commit changes to GitHub.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4cahapxngcynl6mdpwt.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vrgzed1u02s3kvvh05v.png) 

## Typecasting in GO

- Typecasting is a way to convert the variable from one data type to another data type.
- There are two types of data conversions:
- Implicit or Automatic Type Conversions: In Implicit type conversion, compiler automatically converts one data type to another data type. Go language doesn’t support implicit type conversion.
- Explicit Type Conversions: In Explicit Type Conversion, user converts the data type of a variable to required data type. This type of conversion is also called typecasting because the user casts (changes) the data type of the variable.
- To convert a value from one data type to another use the following syntax newDataTypeVariable = T(oldDataTypeVariable). Where T is the new data type.
- To demonstrates the typecasting; create new folder as 02-07 TypeCasting in src folder of the workspace and create typecast.go and type the following code.

{% embed https://gist.github.com/ZammaCode/570059a8370556f1410f6179f0adae58.js %} 

- After execution the the value of f_var will be assigned to integer variable without any error.
- For completion commit changes to GitHub.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mn06i96d4i6gk5fw422k.png)