Basics of Go

Data Types

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

Check out a free preview of the full Basics of Go course

The "Data Types" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates Go's pre-defined data types, including strings, booleans, and numeric types like integers and floating-point values. The discussion also addresses student questions about defining constants globally and Go's ability to automatically determine a variable's data type.

Preview
Close

Transcript from the "Data Types" Lesson

[00:00:00]
>> Data types. We have strings, then we have that like string, that's all? Lowercase is that we have different type for integers we have the standard int, just int, okay, that is actually point. It's an alias to int32, then we have int8, 16, 32, and 64. What's this?

[00:00:25]
The amount of bits that is going to use in memory. That will express actually the minimum and the maximum that you can store. There's of course, this is 2 power 8 power 16, that will give you the amount of different numbers that you can save there. So it will increase precision, they will have versions with the u prefix.

[00:00:53]
You know what that means, don't you? Have ever seen that on any other language? uint. It's unsigned. So it's an unsigned integer. In this case, only positive numbers. Does it make sense? So that means if you set an int8, does the simpler to understand, int8 can go from minus 127 to 128.

[00:01:20]
That's all, and if you use uint8 you're like moving that. I started in zero, from zero 155. So you make a shift, is the same amount of possible numbers that you can store, but you're shifting and starting only with zeros of positive numbers only. So it's natural numbers with zero, including the zero, that's the uint version.

[00:01:51]
If you don't know which one to use, typically use int, and that's all. For floating pole point values, we have two versions, float 32 and float 64. For if you're going to work with GO and the web, like JSON, work with JSON typically you we, we serve JSON, we parse JSON.

[00:02:13]
Every number in a JSON will be converted into float 64. That's why most of the time, you wanna play with that one. Because if not, we will need to make versions, and we might want to make things simple. Then we have bool that is more like boolean is bool.

[00:02:30]
It accepts true or false, lowercase, everything lowercase. And the Boolean operators are the ones that we are used to on any feed based language. So if you're coming from JavaScript, they are the same from C. We have equals, not equals, minor than, greater than, minor equals. This is and, this is or, and this is not, okay?

[00:02:58]
I think that's pretty simple. Also, we have pointers. I know that that will probably give me a big warning in your head first, maybe you know what that is, and lucky you. But if you don't know what that is, if mostly if you're coming from C++, and he's saying with the face, no, no, I don't wanna get there.

[00:03:18]
Well, don't worry because it's not so complicated and go, and you understand the use case for for pointers, and it's not getting too deep or too low level as with C or C++, okay? But actually for now let's say the pointer is we have a variable, and then we have another variable that is pointing to the original one.

[00:03:42]
So you can have the original variable or a variable that is pointing to. It's a link, that is. That is, if you understand that, that's a pointer.
>> Earlier you said I think. Did you say that a constant can only be defined within a function and not, or global.

[00:04:00]
>> No, you can define it constant globally. It's possible.
>> Or a variable possible.
>> I can create a constant. What I cannot use is the shortcut.
>> Okay.
>> I cannot use the shortcut in the global scope. I need to use bar for that. And when we use bar, we don't have the column.

[00:04:18]
That's the only thing that you can not do at the global scope. Just the shortcut.
>> Can go automatically identify the data type.
>> The data type
>> Data type.
>> Yeah. So if you are setting the value at the same time in the same line. So if I'm creating a new variable here, and I say is ready.

[00:04:37]
And I'm applying it through, it's identifying or automatically the type, it's giving me an error because I'm not using it so anxiety. But if I'm doing that, it is okay. So it's actually detecting that if you have equals, but if I'm going to set a value later, it doesn't know what that is.

[00:04:54]
So I have to express that it's going to be a bool. And then you can set that safely later, but if you have the value at the time of declaring the variable, it will implicitly reveal the time.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now