Variables, Constants and Built-in Data Types in Go
ZammaCode
Posted on March 16, 2022
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.
- Following is the output of above code.
- Finally commit changes to your repo and push to GitHub.
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.
- Running the above code produce the following output.
- Commit changes to GitHub.
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.
- Following is the out of above mentioned code.
- Push code to GitHub.
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.
- Commit your code to GitHub.
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.
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.
- Output of the above code snippet.
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.
- On execution of the above code the following error will be raise by the compiler.
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.
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.
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.
- Following is the output.
- Open up Git Bash and Commit changes to GitHub.
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.
- On execution the above code will print the following output.
- Commit changes to GitHub.
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.
- After execution the the value of f_var will be assigned to integer variable without any error.
- For completion commit changes to GitHub.
💖 💪 🙅 🚩
ZammaCode
Posted on March 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
privacy Caught in the Crunch My Journey from Snacks to 2 Million Exposed Users Privacy
November 30, 2024