This course has been updated! We now recommend you take the Basics of Go course.
Transcript from the "Types" Lesson
[00:00:00]
>> So let's get into some of the basic syntax. So we've kinda gotten used to interacting with Go a little bit, our Go files. And let's talk a little bit more about some of the other patterns we're gonna see throughout the day. The first thing I'd like to talk about are the types.
[00:00:11] [BLANK AUDIO] And the way this kind of is organized is the type name is on the left. How we're gonna use it in Go, what that name actually looks like in Go code is in that middle column. And then on the right, we've got examples, and an example of defining a variable using that type.
[00:00:28] We haven't actually talked about defining variables just yet, but we'll work through that shortly. So the first type we'll talk about is an integer. And the type name to use that indoor code is typically int. You'll notice that there are quite a few variations of that including int8, int16 etc.
[00:00:45] And so Go allows you to kind of optimize how much memory you wanna allocate when you're saving variables. In JavaScript, we don't have to worry about that, it'll take care of it. But it also allows, if you're saving a very small variable to a large amount of memory, you have a lot more control over how much actually gets allocated for that variable in Go.
[00:01:01] Today we're gonna be only using just normal int. We're not doing anything with huge amounts of data at the moment, so we won't worry about it too much. And then that uint indicates an unsigned integer which is gonna be a value that's above 0, 0 or above. An integer could be a negative value.
[00:01:20] But examples are integers like 1 and 2, 44. And then to declare a variable, it would be a keyword var to say we're indicating that we're creating a variable. The name of the variable, which in this case is age. And then to the right, we have to specify what type that variable will be now and forever and always.
[00:01:36] So here we're creating a variable called age which is an integer, and it's assigned to a value of 21. The next type we'll talk about is a float. And so the float has two different sizes, float32 or 64. And because we're working with decimal points, these kinda helps you manage how precise you want that decimal to be.
[00:01:55] Sometimes it matters, sometimes it doesn't, based on what you are doing and how much it matters that your mathematical accuracy comes to play for your application. Today we'll probably just use float64 for most of the day. And you'll see that on the right-hand side the examples include 1.5, 3.14 and 2100 although the decimal point isn't there it is point zero behind the scenes.
[00:02:17] And so our variable example is var name gpa which is assigned to a type of float64, and then assigned a value of 4.0. Next up is the string, there is one type name which is just string. You'll notice on the right hand side the example is "Pancakes" and in Go you do need to use double quotes all the time.
[00:02:37] You cannot choose your favorite that day double quotes forever. And then the example variable is a signing of our plant which is a type of string to the value ficus. And lastly our boolean value which is indicated with the bool for Boolean. And this includes all of the conditional operators that look really similar to JavaScript.
[00:02:56] The double end for logical end and or with the straight pipes. The bang operator with the exclamation point less and less than equal, greater than or equal to. The only equality operator with is two equal signs, so you´re not working with the three or two like JavaScript. And then not equal, which is the bang operator and equal sign.
[00:03:14] On the right hand side the words that represent that as examples are true and false. And the variable example here is var, the name of the variable is can drink which is set to a type of Boolean. And it's assigned a value of a conditional where some variable that we've defined elsewhere age is greater than 21, which would return true or false in this case.
[00:03:36] So we're gonna hop back over to the Golang Playground. And additionally, what we wanna talk about here around types is what happens if you don't know what a type is. So as previously mentioned, Go is really particular about, if you're working with a string, you want it to always be a string.
[00:03:49] And when you're working with equality, you can only equate two different, like types of the same kind, like does four equals six. If they're both integers, Go will let you if they're not, you're gonna have trouble. So something that's helpful to know is if you don't know what a variable is, how do you identify the type?
[00:04:05] And if you're working with an int, you want it to be a float or vice versa, how do you convert those types? So we're gonna hop over to the playground to work on that. So besides our front package, which is the only one we've worked with so far, Go also ships with a package called reflect.
[00:04:19] And reflect allows you to dig into the syntax a little bit and ask some questions about what you're typing. So one of our main function here, I'm gonna start with fmt.Println cuz I do wanna see some stuff in the console. But in here, I'm going to use the reflect package to ask for the type of.
[00:04:46] Whatever I wanna pass in the middle here. Obviously we are physically looking at this code we can see that it's a string. But if you couldn't, and you run this code as is, you'll see it prints out the type name, which here is string. Let's say I have a variable defined up here.
[00:05:07] And pretend this is coming from some other file in your code base. I have var x assigned to a value of 4. And if I wasn't sure what that variable was, and I replaced the string here with my variable x, run the program, you'll see it's printing out int.
[00:05:23] Side note about the Go Playground, you'll notice that when I typed this var x, it kinda just shifted over to the left, everything looks really ugly. This Format button on the top here will actually format that for you. So as we kinda work through the playground, if that's irritating, you hit the Format button.
[00:05:40] So okay, we're working with an integer here. But if I wanted to do x times 5.5, and hit Run, we get an error. And it's saying that 5.5 has been truncated to an integer but Go is like not happy about that. And that's because now we're trying to work with two different types.
[00:05:58] We've got an int, we're trying to multiply it by a float, Go doesn't wanna do that at all. So our options are to convert our float to an integer or integer to a float. And in this contrived example, we kinda want that decimal point, we want that precision.
[00:06:12] So instead we're gonna convert our x variable which is an integer 4 to a float. And the cool thing about Go is you can simply use the type name itself as a function. So here we've got float64, and I'm wrapping my x variable in that type. And then when I run it, you should see that our TypeOf is now printing accurately because we were able to multiply the two.
[00:06:32] Similarly, you can convert from float 64 to int. If you're converting things like strings to integers, there isn't a magical parse int built in. But there are additional string libraries that will help you convert additional types back and forth. And that's kind of an interesting part about Go as well.