In Conversation with Go-Lang’s DataTypes | Part2
In case you are landing here directly, it’s suggested that you go and read through this for fundamentals.
Question: What are various variables and language constructs supported by Go-Lang ?
Answer: As with all programming languages you can store data in memory in Go, using variables. Go is a statically typed language. That means that each variable must be assigned to type and once assigned it can’t be changed. You can set types for each variable either explicitly, by naming the type in the variable declaration or implicitly, by allowing the compiler to infer the type based on a value that you initially assign. Go comes with a set of built-in data types. You can also define your own data types but these are the ones that are always available to you.
- The first type is bool, for Boolean. This is a simple true, false value and the only two values that you can assign are true and false.
- String types are collections of characters. A variable with the type of string contains a series of characters. We’ll be talking about strings a lot.
- Integers come in a variety of flavors. These are the fixed integer types. They each declare either an unsigned or assigned integer and the numeric value in the name is the number of bits. That affects the range or the highest and lowest values that you can assign. There’s also a set of type aliases, the byte, the unsigned int, the int and the unsigned int pointer. The int and unsigned int data types are interesting. They reflect either a 32 or a 64 bit value depending on what operating system you’re running on. On macOS and on 64 bit windows these are the same as an int64 and a uint64. But, if you run exactly the same code in the Go playground those become 32 bit values because of the underlying operating system.
- There are two floating types, float32 and float64. And, once again, the numeric values in the names indicate the number of bits used for storage and therefore, the range that’s available to you.
- There are some complex types, complex64 and complex128. A complex number contains two parts, real numbers and imaginary numbers.
- And then there are also built-in types for data collections. There are Arrays and Slices to manage order data collections and Maps and Structs to manage aggregations of values.
- In Go, a function is a type and that’s what makes it possible to take a function and pass it into another function as an argument.
- Interfaces and Channels are also types in Go’s and Go also supports pointers, reference variables that point to an address in memory to refer to another value. These are the built-in types in Go but they’re just the beginning point, because again, you can create your own data types in this language.
Question: How doe we declare the String variable ?
Answer: Following is an example of string data-type creation :-
Question: How doe we declare the Integer variable, What’s the default value of Integer and How do we see the Type of the declared variable ?
Answer: We have functions available in Go-Lang for performing the desired operations :
Question: Can Go, also infer the data-type on its own ? Do we always need to mention the corresponding data-types during variables-declaration ?
Answer: Yes, Go-Lang can definitely infer the data-types too.
Question: How can we receive Input from the Standard-Input ?
Answer: We can receive inputs from the Standard Input like as shown below using 2 packages :0
Note above that :-
- That’s yet another way in Go-Lang to receive-in the values for the variables.
- Underscores are used to ignore errors returned by
Question: What if we wanted to take in Input as Numerals or Floating point numbers through the Standard-Input ?
Answer: We have learnt following packages so far, the receiving inputs is concerned :-
- We get the input from the console using the bufio packages, new reader function and a reader object and the OS.standard in object to get data specifically from what the user types in. These values always come to you as strings.
- We may need to convert them to other types. To do this, I’ll need another package that’s a part of the Go library called strconv for string conversion. And I’m also going to need a package named strings. This package contains all sorts of functions for manipulating strings.
- Now, the user can type-in space characters at the beginning or the end of the string and therefore it won’t parse correctly. So I’m going to use a function from the string package called TrimSpace. And that removes any leading or trailing whitespace.
- We also need to pass-in the bit size. Pass-in 32 if you want to float 32 value or 64 for float 64. I’m working on a 64 bit operating system. So I’ll parse in 64.
Now I need to evaluate that error object. The error will be nil if everything went fine.
Or if there was a parsing problem, it’ll come back as a non nil object which is being described as below :-
Question: Can we directly add the variables of even different-types ?
Answer: We can’t perform the addition :-
Question: How to perform the addition operations between two variables of even different-types ?
Answer: We need to convert the one to another variable, because Matching types are required in math operations.
Question: Is there a way in Go-Lang to round-off the floating point numbers to the nearest Integer automatically ?
Answer: Yes, there is MATH library available in Go-Lang :-
Note above that, the type for “3.7” has been automatically inferred, under the hoods.
Question: What all Operators are supported in the Go-Lang ?
Question: Can we declare multiple variables in a single line ?
Answer: Yes, that’s very much possible in Go-Lang :-
Question: What’s the way to get the result of addition of floating-numbers ?
Question: What’s yet another way of rounding-off to desired precision ?
Question: Can you show me yet another example of rounding-off operation ?
Question: Can you show me yet another example for performing rounding -operation and division-operation with numbers in Go-Lang ?
Question: How can we declare the time, using the time library available in Go-Lang ?
Question: What are other ways of formatting the time, in whatever user-desired format?
Question: How can we declare the particular timeStamp of past/future ?
Question: How can we exit the Program-execution, in case of any error ?
Question: Let’s construct the simple example of Addition-Calculator ?
Please clap now, if you scrolled till here.