2.2 Constants and Comments
There are times when you are writing applications when you will want to store a piece of information that you don’t want to change. While you could definitely use a variable to store that data, it is very possible to forget, make a mistake, and change its value. That’s where constants come in. In this lesson, I’ll show you how these little gems are built for such a situation.
1.Introduction3 lessons, 12:09
2.the Basics8 lessons, 1:15:15
2.1Variables and Types11:28
2.2Constants and Comments04:44
3.Building Web Servers in Go4 lessons, 43:38
3.1Building a Basic Web Server10:44
3.2Serving HTML to the Browser12:27
4.Conclusion1 lesson, 01:32
2.2 Constants and Comments
Now that we have learned a bit about variables in the world of Go, it's time to talk about one of the next evolutions of that concept. And that is creating a variable that doesn't change which sounds kind of strange and that's why we don't call them variables at that point, we refer to them as constants. Now, constants are created and cleared in the world of Go in a very similar fashion that we do variables. So let's start with a simple example. If I were to create a variable, and we'll just call this someString. And we're gonna initialize this to A string. Nothing very exciting here, just a simple example. And then later on, in my application, I can then change that value to be Another string. Like this. So now within my application, I'm simply going to print out some string like that and I can save my application. And let's just go ahead and run it real quick, constants.go, and we get another string. So that's our output. So I've been able to declare and initialize some string to have a value somewhere in my application and that I can updated and change it. As long as it's of the same type, everything will work just fine. But let's say for some reason, I have a situation in my application where I don't want the value of this string or this variable to change, how do I handle that? Well it's actually quite simple, all we have to do is change this var keyword to be const and if I were to change it to be const that obviously represents constant and now there's a few things that happen. I've now instead of creating and declaring initializing a variable, I have now created a constant and then later on in my application I'm not allowed to change that value anymore, it will always be the same. And if I try to change it I'm gonna run into problems. So if I try to run this application again, you're gonna see that I'm gonna get an error, because on line eleven I cannot assign to some string. And the reason that I can't do that is because some string is a constant. There are rules around how this type of a variable, or this constant is used. So now that I've done this, I can limit the access to this constant. It can only be assigned in one spot I can't change it. Now there are a few rules that differentiate how we create variables and constants in the world of the Go programming language and that typically has to deal with the more shorthand notation that you can use for variables. So if you recall I can do someInt and I can use this shorthand notation to initialize it to a five. In the world of constants, we can't do that because the most important part of our declaring of a constant is the const keyword. And there's really no way around that to be able to do that in some sort of shorthand notation. So, we're kinda stuck with this version here, but it's really not all that big of a deal. Now, in addition to constants, there's another very common mechanism used in software development, usually for the developer, known as comments and comments are also supported with EnDo. There are multiple types of comments and we can do them in two forms. We can do a single line comment, and a single line comment in Go is very similar to other languages where we use the double forward slash. So I could say something like this is a package declaration. And just like that, now obviously that's not a very useful comment because if you are learning about the Go programming language you already know that this is a package declaration but you kind of get my point. That's the single line comment now later on in my application or anywhere in my application for that matter if I ever wanted to add a little bit more information or there was some reason that there was a lot going on that I felt was necessary to put a lot of information in about comments. I can use a multi-line comment. So I could do that a couple of different ways, I could either do it several different lines with a double forward slash and that will work. That's typically frowned upon. In most programming languages and Go as well, the way to handle the multi-line comments in a little bit more elegant of a way is using the forward slash star and then you write your comments. Then at the end you get the */, like that. So now everything that I write within this block is a comment. So I could put any sort of text in here that I want, it doesn't make any difference. But it will all be considered comment. So if I were to save this and run again, you're gonna see it's going to continue to work because all of this Gobbledygook here at in the middle for the multi-line comment and at the top, it's all a comment. And so the Go compiler is ignoring that as it goes. So there you have the basics of both constants and comments in the world of Go.