This course has been updated! We now recommend you take the Basics of Go course.
Transcript from the "Variables" Lesson
>> Variables in Go can be defined a few different ways. I'm gonna start working most of the code examples in a actual Go file. I think the syntax highlighting is a little bit more helpful. Feel free to stay in the playground if that's where you find happiness. So let's talk about how variables are defined.
[00:00:18] I'm in the third directory called basic syntax. And we're looking at the variables.go file. And so for right now, the only thing in here is my fem to print line variables line to get going. But the first thing I wanna do is, I wanted to find a variable that kind of the same way we saw it in the types slide.
[00:00:38] And so to do that, we're using the keyword var, whatever we want the name of our variable to be the type we know it will be forever, and then whatever we want to assign that value to. And then if I go ahead and Println(name), I'll hop down to my terminal here and then I go run, we're in package 03_basic_syntax, Code, and then variables.go.
[00:01:07] Make sure you save your file. And then you'll see that Beyonce prints to the console. Go does have inference though. And so right now, you can see that the value on the right-hand side is actually a string. So I can kill this here and Go will know to look to the right-hand side and be like, is this an easily identifiable type?
[00:01:48] So I'm saying that, hey, I want to carve out space for a variable. I know my variable is going to be a string down the road. Not sure what that is yet, but just kind of hang on to it so I can use it later. In this case, because there is nothing on the right-hand side of that assignment operator, the equal sign, we do have to specify the type names that Go knows what kind of type it's going to allocate that space for down the road.
[00:02:09] Cool, so if I save this and now ask what name is, you're gonna see nothing. You're gonna see this like blank space here. And behind the scenes, Go actually assigns default values to any variable that's been instantiated without a value. So if I say, someNumber is going to be an int, and I print some number here, We get a really helpful error.
[00:02:54] So options are to delete it. I'm gonna comment it out for now. And then when I run this code again, my some number variable which is assigned to an integer but has no value is automatically assigned a default value of zero. And this is also goes way of kind of allocating memory.
[00:03:09] If you know it's going to be an integer, I'm going to put a placeholder in there for you. Integer values get zero, strings get an empty string, Boolean is get false, and floats get kind of 0.0. So as we kinda work through stuff, if you're just holding a placeholder with a variable, it'll always have that default value behind the scenes.
[00:04:10] And if I wanted to print both. You'll see that both of those names are saved as variables and printed out. And then the fifth type here is gonna be the most common, and it is a short hand syntax. And to do that, we're going to be back in our function main, this can't happen outside of functions, so it has to be within some sort of function block.
[00:04:36] [COUGH] And it's gonna look something like this. So we can drop that variable keyword, the var, and we can drop the type name assuming that it has a value on the right-hand side. And a colon, equal sign both assigns the variable, associates it with a type without needing a lot of the extra boilerplate.
[00:04:53] So you'll see this most of the time throughout the day.