Go (Golang) Tutorial #3 - Variables, Strings & Numbers
Summary
TLDRThis video tutorial introduces the basics of variable declaration in the Go programming language. It explains how to declare and initialize variables, focusing on strings and integers. The video covers multiple methods of variable declaration, including explicit typing, inferred typing, and shorthand syntax. It also discusses the use of integers, their bit sizes, and floats, highlighting the differences between types like int, uint, and float32/64. Additionally, the tutorial emphasizes best practices for using variables and resolving common errors in Go programming.
Takeaways
- 😀 Variables in Go can be declared using the `var` keyword, followed by the variable name, its type, and a value.
- 📚 Strings in Go are defined using double quotes, not single quotes.
- ⚠️ In Go, declared variables must be used, or you'll encounter an error.
- 💡 Go supports type inference, meaning the language can automatically determine the variable type based on its value.
- 📝 You can declare a variable without assigning it a value, in which case Go assigns it a default value.
- 🔄 Variables can be updated, but their types cannot be changed once declared.
- ⏩ Go offers a shorthand for variable declaration using `:=`, but this can only be used within functions.
- 🔢 Go has multiple integer types (e.g., `int8`, `int16`, `int32`), allowing for different ranges based on memory size.
- ➖ Unsigned integers (`uint`) in Go do not allow negative values and have a different range from signed integers.
- 🌊 Floats are used for decimal numbers in Go, and their precision can be specified using `float32` or `float64`.
Q & A
What is a variable in Go?
-A variable in Go is used to store information or data such as strings, numbers, arrays, etc. Variables must be declared before being used, and they have specific types such as string or int.
How do you declare a string variable in Go?
-A string variable in Go can be declared using the `var` keyword, followed by the variable name, the type `string`, and the value. For example: `var name1 string = "mario"`.
What happens if you declare a variable but don't use it in Go?
-In Go, if you declare a variable but don't use it, the compiler will throw an error. You need to use every declared variable to avoid this error.
What is type inference in Go?
-Type inference in Go allows the compiler to automatically determine the type of a variable based on the value it is assigned. For example: `var name2 = "luigi"` infers that `name2` is of type string.
How can you declare a variable without initializing it in Go?
-You can declare a variable without initializing it by specifying the type but not assigning a value, e.g., `var name3 string`. The variable will have a default value, which for strings is an empty string.
What is the shorthand for declaring a variable in Go?
-The shorthand for declaring a variable in Go is to use `:=` without the `var` keyword. For example: `name4 := "yoshi"` declares and initializes the variable `name4` as a string.
Why can't you use the shorthand variable declaration outside of functions in Go?
-The shorthand declaration using `:=` can only be used within functions in Go. If used outside of a function, the compiler will throw an error because it's not allowed in the global scope.
What is the difference between int and float in Go?
-In Go, an `int` is used for whole numbers, whereas a `float` is used for numbers with decimal points. For example, `var age int = 20` for an integer and `var score float64 = 21.5` for a floating-point number.
How does Go handle different bit sizes for integers?
-Go allows specifying the bit size for integers, such as `int8`, `int16`, `int32`, or `int64`. The larger the bit size, the larger the range of values that the integer can hold. For example, `int8` can hold values from -128 to 127, while `int16` has a larger range.
What is a uint in Go and how is it different from int?
-A `uint` is an unsigned integer in Go, meaning it can only hold non-negative values (no negatives). For example, `uint8` holds values from 0 to 255, unlike `int8`, which holds both negative and positive values.
Outlines
💻 Understanding Variables in Go Programming
The paragraph introduces basic concepts of variables in the Go programming language, particularly focusing on how to declare variables. It explains that variables are used to store data like strings, numbers, and arrays. The paragraph demonstrates the explicit declaration of a string variable using `var`, along with Go's requirement to use double quotes for strings. It highlights Go's strict type checking, where attempting to assign an incorrect type (e.g., a number to a string) results in an error. The use of the `fmt.Println()` function to print variables and avoid declaration errors is also discussed.
🔀 Different Ways to Declare Variables in Go
This paragraph describes three different ways to declare variables in Go. First, it shows explicit typing where the variable type is declared along with a value. Second, it introduces type inference, where Go automatically determines the variable type based on the assigned value. Lastly, it covers the declaration of a variable without immediately assigning a value, which results in a default value (e.g., an empty string for strings). The paragraph ends with examples of reassigning values to variables and an introduction to the shorthand syntax for variable initialization.
📝 Shorthand Variable Declaration in Go
Here, the focus shifts to shorthand variable declaration using the `:=` syntax. The paragraph explains that this method allows Go to infer the variable type without using the `var` keyword. However, it can only be used for the first variable assignment inside functions. The text emphasizes that this method is frequently used in Go for convenience, but it cannot be applied outside function scopes. The paragraph concludes by demonstrating how to print the variable values and remove errors that occur when variables are declared but not used.
🔢 Working with Integers in Go
The paragraph begins a discussion about handling numbers in Go, specifically integers (whole numbers) and floats (decimal numbers). It details how to declare integers using either explicit typing or type inference, with examples of printing out integer values. Additionally, the paragraph introduces the concept of specifying bit sizes for integers (e.g., `int8`, `int16`, `int32`, `int64`) and explains how the size affects the range of values a variable can hold. The paragraph also mentions Go’s enforcement of these size limits and how errors occur when values exceed the defined range.
💡 Unsigned Integers in Go
This section introduces unsigned integers (`uint`), which differ from signed integers by not allowing negative values. It explains that unsigned integers have a larger positive range since negative values are not represented. The paragraph provides examples of `uint8`, showing the allowable range and explaining how to select different bit sizes (`uint16`, `uint32`, etc.) based on the required number range. The concept of needing specific bit sizes is discussed but with the reassurance that general applications often do not require this level of precision.
🔄 Working with Floats in Go
The paragraph covers float types in Go, which represent numbers with decimal points. It explains that, unlike integers, floats require specifying bit sizes (`float32` and `float64`). The larger the bit size, the more precision and range the float can handle. The paragraph also mentions that for most applications, `float64` is used due to its higher precision. Additionally, it clarifies that type inference automatically defaults to `float64` when using shorthand declaration for float variables. It concludes by comparing the behavior of floats and integers, particularly the prohibition of decimal points in integer types.
Mindmap
Keywords
💡Variable
💡String
💡Integer (int)
💡Type Inference
💡fmt package
💡Declaration
💡Type Safety
💡Bit Size
💡Float
💡Colon equals (:=) operator
Highlights
Introduces the basics of programming, focusing on variables to store information like strings and numbers.
Demonstrates how to declare a string variable explicitly by specifying the type and assigning a value.
Explains that Go uses double quotes for strings and the importance of using declared variables to avoid errors.
Shows how to use fmt.Println to print variables to the console, resolving unused variable errors.
Introduces variable declaration with type inference, where Go determines the variable type based on the assigned value.
Discusses the default value of an uninitialized string variable as an empty string.
Illustrates updating the value of a string variable and initializing an uninitialized variable.
Presents shorthand variable declaration using := for type inference and assignment in one step.
Highlights the limitation of using shorthand declaration only within functions, not outside.
Demonstrates integer declaration with explicit type specification and type inference.
Explains the difference between integers (whole numbers) and floats (numbers with decimal points).
Shows how to declare integers with specific bit sizes (e.g., int8, int16) and the impact on the range of values.
Introduces unsigned integers (uint) which can only store non-negative values.
Describes the declaration and usage of floats (float32 and float64) for numbers with decimal points.
Highlights that default type inference for floating-point numbers uses float64 for higher precision.
Transcripts
all right then gang so hopefully you
already know
the very basics of programming and you
know what a variable is
we use it to store information or data
like strings numbers arrays etc
now for now we're going to focus on
strings and numbers as well as how to
declare
variables in go so let's start off with
a few strings now there's various ways
we can declare
variables in go the first way is to say
var
and then the name of the variable which
i'm going to call name name1
and then we say the type of this
variable so i'm going to say
string and then we set that equal to a
string now if i try to set this equal to
a number
then i'm going to get an error because a
number is not of type
string this is an integer so this has to
be a string now because we're typing it
explicitly
right here so strings in go are double
quotes
we cannot use single quotes strings are
double quotes
so let me say that this value is mario
now notice we get an error over here and
that's because we're declaring this
variable
but it's saying to us if we hover over
this we've declared it but we're not
using it and that is an error in go so
in order to get rid of that error we can
come down here and say
fmt dot prince
line which we saw in the last lesson and
i'm gonna say
name one and as soon as we use that
variable the error goes away
and by the way if i comment out this
line we also get a similar error up here
where we import
fmt and if we hover over that we can see
fmt is imported
but not used so the minute we use it
that
error goes away all right so if i save
this and open up a terminal
i'm gonna run this main.go file again
and we should see name one in the
console mario we do
all right so that's one way to declare a
variable var
then the variable name then the type and
then we set it equal to something
the next way is to say var and we'll
call this name
to and then we don't type it straight
away we just set it equal to something
i'm going to set it equal to a string
again luigi
and what happens is go will look at this
and it will
infer the type automatically so it will
say look i'm reading this and i can see
it's a string
so this and this line are pretty much
the same it's just that here we're
explicitly typing it
and here go is automatically inferring
the type
for us if we hover over name two we can
see
at the top it says var name two is a
string so this does
the same thing and if i tried to change
name two to something different like an
integer
in the future it wouldn't let me because
this is still a string so
we can output this as well name two i'm
not going to run the program just yet i
want to show you a third way
so var name three
and then i'm gonna say string like so
and this time
i'm not going to give it a value all i'm
doing
is setting up the variable if you like
for future use so in the future we can
use
name three and it can only ever be a
string but right now
it doesn't have a value or at least not
a value that i've given to it
so if we output these name three at the
end
i'm going to save it and run the program
so we can see mario and luigi and the
third one over here
you can't see it but it's just an empty
string that's the default value
for a string when we don't give it a
value ourselves
all right then so what if i want to
update one of these well i can
i can come down here and say name one is
equal to something else
again i can't change the type i can't
say 25
that's not a string and we get an error
but i can change it to a different
string for example
peach and also i could take name 3
and give that a value for the first time
so i can say name
three is equal to browser
like so and then if i print out these
things again
let me copy this paste it down here
and run this we should see the first
values and then the second values
so mario and luigi then peach luigi and
bowser awesome so that all works
alright then so there is another way
that we can initialize a variable
so let me show you this i'm going to say
now without
the var keyword name for
and then i say colon equals and then
string so yoshi so this is basically
a shorthand for either this
or this so we don't use the var key word
and instead
we use the colon and we only do this the
first time we're initializing or
declaring the variable
i wouldn't then update it later on and
use colon only
the first time all right so this does
the same thing
go automatically infers this type to be
a string and we can see that if we hover
over it says
var name four is a string so this is
just
a shorthand version and this is what
i'll be using
for most of the variables in this
tutorial series
now one quick word of warning you can't
use this
outside of a function so later on we'll
see
variables declared outside of the
function somewhere
in this file or in other files so i
could say up here
var and then some name
is equal oops to a string right
like this now i can't use this shorthand
up here outside of a function so if i do
this
get rid of that and use colon equals
then it's not going to let me do this we
get an error so just bear that in mind
but inside a function pretty much all
the time i'll be using this
all right so we still get an error
because we're not using name four so
let's just print it out i'm going to say
under here fmt
dot print line and it's going to be name
for oops if i can spell it save that
open up the terminal
and run this and we should see
also yoshi down here which we do awesome
all right so next up let's talk about
integers
so when we're working with numbers in go
we have two different types we have
ins for integers they're whole numbers
and we have floats
which are for numbers with decimal
points in them
so we're going to look at ins first of
all then we'll come back to floats
so the way we declare these is pretty
much the same way as we declare these
things up here
we can either say var and then we'll say
age
1 is an int that's the type and set that
equal to
20. all right so that works we can have
var age 2 and set that equal to
30 and this time again go is going to
infer the type
if we hover over h2 we can see it's an
int type
we are getting those errors because
we're not using these variables yet but
they'll go away later
and again we can use this kind of
shortcut right here
so let's say age three without the var
keyword
and then colon equals 40. all right so
all of those three are valid
so i'm going to come down here and print
out these so
print line and we're gonna output age
one age two and age
three and in fact what i'm also gonna do
is just comment out a couple of these
lines and variables so we're not
flooding the console
so i'm going to save that and open up
the terminal
run the file again and hopefully now we
can see all of those
integers so 20 30 and 40.
now we can also use variations of this
in type to specify the bit size
of the integer for example we might want
our integer to be 8 bits or 16 bits etc
now the larger the number we use for the
variable
the higher the number of bits you're
going to be needing so
let's do some examples and we'll talk
about it a bit more
so i'm going to say down here bits and
memory and then what we'll do is create
a variable
num one and that is going to be of type
int
and then we put an eight next to it and
that means eight bits
and i'm gonna set that equal to 25. now
there will be a specific
range of numbers that we can use when we
say
8 bits now if i scoot over the
documentation and i'll leave this link
down below we can see down here type int
16 type int 32 int
64 and int 8 so this is the one we're
using right
here and you can see that we can have
the numbers which range from
minus one to eight to a hundred and
twenty seven so
25 is in that range that we used but if
we try to use plus one to eight
then it's not going to allow us to do
that so let me just scoot this back over
here for now
if i try to use one two oops two on five
that'll do
then we get an error and that's because
it's outside of the scope of int
eight we can't have a number this big
however if i change this to
in 16 that's going to be absolutely fine
because again if we look over here
int 16 has this much larger range
in 32 has a larger range still and in
64 has a huge range all right
so that's these different ins types
let's leave this at 25 and change this
back to
eight now underneath i'm going to say
var num 2 is equal to
minus one two eight now this is allowed
but if i go to minus one two nine
then it's not going to be allowed
however i need to declare that this is
an int eight first of all you can see
now
we get a squiggly line but one two eight
is absolutely
fine all right then so let's try
another type of int which is an
unassigned int
so i'm gonna say var and this is gonna
be
num three and i'm going to set that
equal to be
a u int which is an unsigned int and
this basically means
we can't have a negative number so i
could set that equal to
plus 25 for example and that's fine but
if i try
to set this equal to minus 25 i get an
error
and again we can declare the bit size of
you ins so i could say
eight for this now whereas before we
could go
to 127 but not above now with unt we can
go
beyond that because we're not including
minuses so all the minuses
are now going over to the plus side so
we get those extra
numbers available to us so for example
if i go to
uint which is down here somewhere
then you can see oops it doesn't specify
the range for uint's eight there oh
there it is
zero two two five five so now i could go
all the way up to two five five that's
okay if i go to two five six
not going to work and i have to go up a
bit size to uint
sixteen all right so hope that all makes
sense
so that's integers the different sizes
of integers and unsigned
integers as well now most of the time
and pretty much all the time
for this application we'll be making
later on we're not going to specify
the number of bits and you probably
won't need to most of the time unless
you need to be
really specific about how many bits you
want your integer to be it needs to be a
specific size
we're just going to go with int for most
of the time
all right so the last thing i want to
show you is a float and remember a float
is a number which has a decimal point so
something like 21.5
would be a float so i'm going to say var
score
1 and we'll set that to be a float and
now unlike integers we have to specify
the bit size
and it can be either 32 or 64. so again
the bit size
dictates the range of numbers we can use
and the higher amount of bits
the larger the range so float 32
and it can be plus or minus so i could
do minus 1.5 that's fine
or i could do just plus and then
something like 25.98
we can also do a float 64. so far score
one or two rather this time and that is
going to be a float
64 and it could be some stupidly large
number really doesn't matter okay all
right then so
for the most part we'll be using float64
because
they have a slightly higher precision
than float32s as well
and to be honest it's not going to cause
much of a memory hit
on your computer so we'll be using
float64 for the most time
and in fact if you just use this
operator right here
whereby it's inferred the type that will
be
the default type so i could say for
example
score three colon equals
to i don't know 1.5 that's going to be
inferred
as a float 64. if i hover over this
we can see var score 3 float 64. so this
is the kind of thing we'll be doing in
the future all right
and just to demo that we can't have
decimal points on integers if i change
this to
20.5 you can see we get an error
there for whole numbers only all right
okay then so if you want to see all the
ranges and whatnot i'm going to leave
this link down below the video and you
can see all the different ranges for the
ins and the floats etc
right here but next up in the next video
we're going to talk a little bit more
about this fmt
package right here and the different
methods we can use on that to print
strings
to the console
5.0 / 5 (0 votes)