Golang Constants & Unsigned Constants

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
contents are everywhere in any sizeable programming projects and google projects are no exception however they are implemented quite differently in go compared to other programming languages and that's today's topic so you can use them correctly [Music] what's up ninjas welcome back to the golang dojo this channel is all about the gold programming language and becoming golan ninjas together and for the next few dozens of videos i'll be going through some of the most popular technical books that are related to the golang so if you want to learn the gold programming language but you hate reading technical books this channel is for you because i'll be doing all that work for you so make sure to join the golang dojo by hitting that subscribe button and let's get on the journey in becoming golan ninjas and mastering her gold programming language together here we have a hello world program open on the go lana by herjet's brain so if we run the game and go run i mean i go which is the file name hershey would be able to see that hello world getting put it out here and we can do the same by hitting this a triangle button and the current theory that we are in right now we are going through the book the go programming language so with all that set up let's get started with today's topic now let's go through the most simple way to declare a constant and then we're going to talk about why exactly do we want a constant compared to let's say a variable for example so how you would declare a constant in go is typing in const and then the name of the constant and then actually assign a value to that constant we can go ahead and print this out and if we run it we should be able to see that it is indeed printing out five which is the value being assigned to this constant and there's a in a type integer type have been assigned implicitly and this is exactly the same of of how you would declare a variable if you haven't checked that out how to declare a variable make sure to check out my previous video so this is how you would declare a constant and in contrary to a variable for example so why do we actually want a constant if we actually declare a variable called six there's also an integer type being assigned implicitly if we don't put this into here and if we actually try to put out six here we should be able to see that it is indeed putting out six and the problem with the the difference between a variable saying constant is that you can then reassign uh a value to the variable and that one should be able to give us a different value for six here and as you can see 10 and being reassigned it to the variable called six it doesn't really make sense and that is the perfect case of a concept to come into play because you cannot reassign the values to a constant so that once you have already declared a consonant then you can't change it then there's unless chances of people using your content incorrectly one other use case of a constant is obviously we still want to store values that aren't going to change but something that we want to do with a constant is that some values are harder to rewrite than others it would be nice if we actually can store it in a constant so that we don't have to rewrite that value over and over again such a one example of that will be pi in math obviously we can still assign a three-digit value 3.014 to pi but then we all know that a pi is an infinite number that has an infinitely longer tail of digits after the decimal number than just three digits that we have here it would be pretty nice if we can actually uh store that value that constant value into a maybe like a built-in library like math and that is exactly that is exactly what's been done and as you can see there is a um much much more position in uh this constant than our and then the constant that we just defined here and this is extremely useful if we need position we can also declare multiple constants within the same line of instruction by using the parentheses so if we actually declare a equals to one b equals to two and so on and actually printing this out we can indeed run this instruction and see that it is printed out one two three four here one other thing that you can do is by using these parentheses is that you can actually get rid of these declarations and what you will see is it will print one one and three 3. so this one is going to take whatever is being assigned previously and this one will be taking whatever it is being assigned here and then the d will just take the previous assignment however what if you do want to have a one two three four as we previously had it but then you or any other incrementing values but then you don't want to write that explicitly every single time and so this is the perfect use case for iota so if we actually try to maybe start from zero we can actually do ends equal to ioda and as you can see out is equal to zero and then you can do one and then do two then the compiler will just automatically set these values for you so if you run the program we can indeed see that it is starting from a zero and incrementing afterwards with the use of iodine however maybe you don't necessarily want to start from zero and you can absolutely do that maybe you want to start from five then you can just add five here and then as you can see it is starting from five and then incrementing afterwards aside from that so we can actually do something a little bit more complicated and more interesting with iota and we are going to go over an example here and and b gb and so on and now let's just print out the first of a values and see what kind of numbers that we are getting and i actually run the program as you can see it is putting out 10 and 24 which is at the number of bytes for kb and incremental amounts for mb and gb and as for there's a formula this is a binary shift to the left 10 times which is the incremental amount of a kb to mb to gb and so on one thing that we should take note of when it comes to constants in go is that all of these values are actually untyped a constant and what do i mean by a type a constant so if i actually try to print out the very last value here which is yb and we should be able to get a overflow for a an integer because the value that we are assigning here is actually much larger than what an integer uh can hold what an integer type can hold however we are able to perform other operations on these constants such as divisions between these constants and if we actually comment this one out and run the program as you can see it is indeed giving us a integer value as long as that the result of the division is not going to give us a number that is going to overflow the integer type again the reason being is how constants are implemented in a go so if we actually try to to check out how it is being implemented so as you can see integer values are actually big integers which is um a nats and a nas is a in a way of words words are just unsigned integers so in order to store potentially large values um as untyped constants and we're actually storing them and not as integers per se but actually an array of unsigned uh integers now one suggestion that you might make at this point in order for us to potentially use a very large numbers for this constant is that why don't we just declare a font of what these types are going to be and maybe we can use a libraries like the math library that we talked about in our last video in order to make that happen for example if i actually try to print out math the power 2 to the power of a 100 this is definitely going to give us a very large number right and if we run the program it indeed it is going to return us a very larger number the problem though with this if we actually try to declare a constant with functions like this it's not going to work because if we just have a constant of a very large number here equals to math at our power 2 to the power of 100 it's actually not going to work it's going to give us an area that this is not a constant the this method is now returning as a constant that is because amount of that power is getting calculated during run time and then it's going to return a variable instead of a constant which a value is getting assigned during compile time because of such limitations with constant the same limitations is going to apply to when we are trying to utilize iota to declare our constants automatically with incremental values for that reasons sometimes using a variable might make your life easier even though conceptually it might make more sense to use a constant last but not least some common concepts that we can utilize that are included in our built-in libraries for example are going to be math.pi as we talked about it may be like time you want to have represent a specific month during the year and maybe as a time unit or even just a time zone uh some other things would be the big library that we just talked about the max exponents that is supported in the big library the minimum exponent that is supported by the the big library and the list goes on but these are just some of the common ones that you can absolutely utilize so that wraps it up for chapter three in the book the goal program language if you want to continue learning more about the go programming language but you hate reading technical books this channel is for you because i'll be doing that for you for the next few dozens of videos going through some of the most popular technical books under the sun related to the golan so make sure to join the golang dojo by hitting that subscribe button and with that a shot again in our next video [Music] you
Info
Channel: Golang Dojo
Views: 936
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, go tutorial, golang tutorial for beginners, golang for beginners, golang constants, golang unsigned constants, golang constants and unsigned constants, constants in golang, unsigned constants in golang, go constants, go unsigned constants, golang constants tutorial, golang variable and contants, constant, constants in go, golang constant tutorial, go constant tutorial, golang unsigned constants explained
Id: yz_rG-b55UY
Channel Id: undefined
Length: 12min 15sec (735 seconds)
Published: Sat Apr 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.