Go (Golang) Tutorial #3 - Variables, Strings & Numbers

Net Ninja
20 May 202113:53

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

00:00

💻 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.

05:01

🔀 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.

10:01

📝 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

A variable is a storage location in programming that holds data, such as strings, numbers, or arrays. In the context of the video, the speaker explains that variables are used to store information, and Go provides different ways to declare variables, including specifying their type explicitly or allowing Go to infer the type based on the value assigned.

💡String

A string is a data type used to represent text in programming. In Go, strings must be enclosed in double quotes. The video demonstrates how to declare string variables, such as 'name1' being assigned the value 'mario,' and explains that using single quotes will result in an error.

💡Integer (int)

An integer is a data type that represents whole numbers. In the video, integers are used to store values like age (e.g., age1 = 20), and the speaker explains how Go allows you to declare integers with different bit sizes, such as int8 or int16, which determine the range of numbers they can store.

💡Type Inference

Type inference is when Go automatically determines the data type of a variable based on the value assigned to it. In the video, the speaker shows that Go can infer the type of variables, such as 'name2 = luigi' where Go recognizes that the variable is a string without needing explicit declaration.

💡fmt package

The fmt package in Go provides formatted input and output functions, such as printing data to the console. The video uses fmt.Println() to print the values of variables, and the speaker emphasizes how failing to use imported packages like fmt results in an error until they are utilized in the code.

💡Declaration

Declaration refers to defining a variable or other programming construct in Go. In the video, different ways to declare variables are discussed, such as using the 'var' keyword followed by a type or using shorthand notation (:=). The speaker also explains how variables can be declared without immediate assignment, leaving them with default values.

💡Type Safety

Type safety ensures that variables are only assigned values of the correct type. In Go, this is enforced strictly, as seen in the video where assigning an integer to a string variable results in an error. Type safety prevents bugs by ensuring data types are correctly handled in the code.

💡Bit Size

Bit size refers to the amount of memory allocated for a data type, which determines its range of values. In the video, int8, int16, and uint8 are examples of integers with specific bit sizes, with int8 allowing values from -128 to 127 and uint8 allowing values from 0 to 255. The speaker emphasizes how larger bit sizes allow for larger number ranges.

💡Float

A float is a data type that represents numbers with decimal points. The video contrasts floats with integers, showing how floats are declared with bit sizes (e.g., float32, float64), and are useful for storing values like 25.98. The speaker explains that float64 is typically preferred for higher precision.

💡Colon equals (:=) operator

The ':=' operator is shorthand for declaring and initializing variables in Go. It allows a variable to be declared without specifying its type explicitly. The video demonstrates how this operator is used to simplify variable declaration, as in 'name4 := 'yoshi,' where Go automatically infers the type as string.

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

play00:02

all right then gang so hopefully you

play00:03

already know

play00:04

the very basics of programming and you

play00:06

know what a variable is

play00:07

we use it to store information or data

play00:09

like strings numbers arrays etc

play00:12

now for now we're going to focus on

play00:13

strings and numbers as well as how to

play00:15

declare

play00:16

variables in go so let's start off with

play00:19

a few strings now there's various ways

play00:22

we can declare

play00:23

variables in go the first way is to say

play00:25

var

play00:26

and then the name of the variable which

play00:28

i'm going to call name name1

play00:30

and then we say the type of this

play00:31

variable so i'm going to say

play00:33

string and then we set that equal to a

play00:35

string now if i try to set this equal to

play00:38

a number

play00:39

then i'm going to get an error because a

play00:40

number is not of type

play00:42

string this is an integer so this has to

play00:45

be a string now because we're typing it

play00:47

explicitly

play00:48

right here so strings in go are double

play00:51

quotes

play00:52

we cannot use single quotes strings are

play00:55

double quotes

play00:56

so let me say that this value is mario

play00:59

now notice we get an error over here and

play01:02

that's because we're declaring this

play01:03

variable

play01:04

but it's saying to us if we hover over

play01:07

this we've declared it but we're not

play01:09

using it and that is an error in go so

play01:12

in order to get rid of that error we can

play01:14

come down here and say

play01:16

fmt dot prince

play01:19

line which we saw in the last lesson and

play01:22

i'm gonna say

play01:23

name one and as soon as we use that

play01:25

variable the error goes away

play01:27

and by the way if i comment out this

play01:29

line we also get a similar error up here

play01:31

where we import

play01:32

fmt and if we hover over that we can see

play01:35

fmt is imported

play01:36

but not used so the minute we use it

play01:39

that

play01:40

error goes away all right so if i save

play01:43

this and open up a terminal

play01:45

i'm gonna run this main.go file again

play01:48

and we should see name one in the

play01:51

console mario we do

play01:53

all right so that's one way to declare a

play01:55

variable var

play01:57

then the variable name then the type and

play01:59

then we set it equal to something

play02:00

the next way is to say var and we'll

play02:03

call this name

play02:04

to and then we don't type it straight

play02:07

away we just set it equal to something

play02:10

i'm going to set it equal to a string

play02:11

again luigi

play02:13

and what happens is go will look at this

play02:16

and it will

play02:17

infer the type automatically so it will

play02:20

say look i'm reading this and i can see

play02:22

it's a string

play02:23

so this and this line are pretty much

play02:25

the same it's just that here we're

play02:27

explicitly typing it

play02:29

and here go is automatically inferring

play02:32

the type

play02:33

for us if we hover over name two we can

play02:35

see

play02:36

at the top it says var name two is a

play02:38

string so this does

play02:40

the same thing and if i tried to change

play02:42

name two to something different like an

play02:44

integer

play02:45

in the future it wouldn't let me because

play02:47

this is still a string so

play02:50

we can output this as well name two i'm

play02:53

not going to run the program just yet i

play02:55

want to show you a third way

play02:57

so var name three

play03:00

and then i'm gonna say string like so

play03:03

and this time

play03:04

i'm not going to give it a value all i'm

play03:06

doing

play03:07

is setting up the variable if you like

play03:10

for future use so in the future we can

play03:12

use

play03:13

name three and it can only ever be a

play03:15

string but right now

play03:16

it doesn't have a value or at least not

play03:18

a value that i've given to it

play03:20

so if we output these name three at the

play03:24

end

play03:24

i'm going to save it and run the program

play03:28

so we can see mario and luigi and the

play03:31

third one over here

play03:32

you can't see it but it's just an empty

play03:34

string that's the default value

play03:37

for a string when we don't give it a

play03:39

value ourselves

play03:40

all right then so what if i want to

play03:42

update one of these well i can

play03:44

i can come down here and say name one is

play03:46

equal to something else

play03:47

again i can't change the type i can't

play03:50

say 25

play03:51

that's not a string and we get an error

play03:53

but i can change it to a different

play03:54

string for example

play03:56

peach and also i could take name 3

play03:59

and give that a value for the first time

play04:01

so i can say name

play04:04

three is equal to browser

play04:08

like so and then if i print out these

play04:10

things again

play04:11

let me copy this paste it down here

play04:15

and run this we should see the first

play04:17

values and then the second values

play04:19

so mario and luigi then peach luigi and

play04:22

bowser awesome so that all works

play04:25

alright then so there is another way

play04:27

that we can initialize a variable

play04:30

so let me show you this i'm going to say

play04:32

now without

play04:33

the var keyword name for

play04:36

and then i say colon equals and then

play04:39

string so yoshi so this is basically

play04:43

a shorthand for either this

play04:46

or this so we don't use the var key word

play04:49

and instead

play04:50

we use the colon and we only do this the

play04:53

first time we're initializing or

play04:54

declaring the variable

play04:56

i wouldn't then update it later on and

play04:59

use colon only

play05:00

the first time all right so this does

play05:03

the same thing

play05:04

go automatically infers this type to be

play05:07

a string and we can see that if we hover

play05:09

over it says

play05:10

var name four is a string so this is

play05:13

just

play05:13

a shorthand version and this is what

play05:15

i'll be using

play05:16

for most of the variables in this

play05:19

tutorial series

play05:21

now one quick word of warning you can't

play05:24

use this

play05:25

outside of a function so later on we'll

play05:28

see

play05:29

variables declared outside of the

play05:31

function somewhere

play05:32

in this file or in other files so i

play05:34

could say up here

play05:36

var and then some name

play05:39

is equal oops to a string right

play05:43

like this now i can't use this shorthand

play05:47

up here outside of a function so if i do

play05:49

this

play05:50

get rid of that and use colon equals

play05:53

then it's not going to let me do this we

play05:55

get an error so just bear that in mind

play05:58

but inside a function pretty much all

play06:00

the time i'll be using this

play06:02

all right so we still get an error

play06:03

because we're not using name four so

play06:05

let's just print it out i'm going to say

play06:07

under here fmt

play06:09

dot print line and it's going to be name

play06:13

for oops if i can spell it save that

play06:16

open up the terminal

play06:18

and run this and we should see

play06:22

also yoshi down here which we do awesome

play06:25

all right so next up let's talk about

play06:27

integers

play06:28

so when we're working with numbers in go

play06:30

we have two different types we have

play06:32

ins for integers they're whole numbers

play06:34

and we have floats

play06:35

which are for numbers with decimal

play06:38

points in them

play06:39

so we're going to look at ins first of

play06:40

all then we'll come back to floats

play06:42

so the way we declare these is pretty

play06:44

much the same way as we declare these

play06:46

things up here

play06:47

we can either say var and then we'll say

play06:50

age

play06:51

1 is an int that's the type and set that

play06:54

equal to

play06:55

20. all right so that works we can have

play06:58

var age 2 and set that equal to

play07:01

30 and this time again go is going to

play07:03

infer the type

play07:04

if we hover over h2 we can see it's an

play07:07

int type

play07:08

we are getting those errors because

play07:09

we're not using these variables yet but

play07:11

they'll go away later

play07:12

and again we can use this kind of

play07:15

shortcut right here

play07:16

so let's say age three without the var

play07:19

keyword

play07:20

and then colon equals 40. all right so

play07:23

all of those three are valid

play07:25

so i'm going to come down here and print

play07:27

out these so

play07:29

print line and we're gonna output age

play07:33

one age two and age

play07:37

three and in fact what i'm also gonna do

play07:41

is just comment out a couple of these

play07:43

print

play07:44

lines and variables so we're not

play07:46

flooding the console

play07:47

so i'm going to save that and open up

play07:49

the terminal

play07:51

run the file again and hopefully now we

play07:53

can see all of those

play07:54

integers so 20 30 and 40.

play07:58

now we can also use variations of this

play08:01

in type to specify the bit size

play08:03

of the integer for example we might want

play08:05

our integer to be 8 bits or 16 bits etc

play08:09

now the larger the number we use for the

play08:11

variable

play08:12

the higher the number of bits you're

play08:14

going to be needing so

play08:15

let's do some examples and we'll talk

play08:17

about it a bit more

play08:18

so i'm going to say down here bits and

play08:22

memory and then what we'll do is create

play08:24

a variable

play08:25

num one and that is going to be of type

play08:28

int

play08:28

and then we put an eight next to it and

play08:30

that means eight bits

play08:32

and i'm gonna set that equal to 25. now

play08:34

there will be a specific

play08:36

range of numbers that we can use when we

play08:38

say

play08:39

8 bits now if i scoot over the

play08:42

documentation and i'll leave this link

play08:44

down below we can see down here type int

play08:47

16 type int 32 int

play08:50

64 and int 8 so this is the one we're

play08:53

using right

play08:54

here and you can see that we can have

play08:56

the numbers which range from

play08:57

minus one to eight to a hundred and

play09:00

twenty seven so

play09:01

25 is in that range that we used but if

play09:03

we try to use plus one to eight

play09:05

then it's not going to allow us to do

play09:06

that so let me just scoot this back over

play09:08

here for now

play09:09

if i try to use one two oops two on five

play09:13

that'll do

play09:14

then we get an error and that's because

play09:16

it's outside of the scope of int

play09:18

eight we can't have a number this big

play09:20

however if i change this to

play09:22

in 16 that's going to be absolutely fine

play09:24

because again if we look over here

play09:26

int 16 has this much larger range

play09:30

in 32 has a larger range still and in

play09:33

64 has a huge range all right

play09:37

so that's these different ins types

play09:39

let's leave this at 25 and change this

play09:42

back to

play09:43

eight now underneath i'm going to say

play09:46

var num 2 is equal to

play09:50

minus one two eight now this is allowed

play09:53

but if i go to minus one two nine

play09:56

then it's not going to be allowed

play09:57

however i need to declare that this is

play09:59

an int eight first of all you can see

play10:01

now

play10:01

we get a squiggly line but one two eight

play10:04

is absolutely

play10:05

fine all right then so let's try

play10:09

another type of int which is an

play10:11

unassigned int

play10:12

so i'm gonna say var and this is gonna

play10:14

be

play10:15

num three and i'm going to set that

play10:18

equal to be

play10:19

a u int which is an unsigned int and

play10:22

this basically means

play10:23

we can't have a negative number so i

play10:27

could set that equal to

play10:28

plus 25 for example and that's fine but

play10:31

if i try

play10:32

to set this equal to minus 25 i get an

play10:35

error

play10:35

and again we can declare the bit size of

play10:37

you ins so i could say

play10:39

eight for this now whereas before we

play10:42

could go

play10:42

to 127 but not above now with unt we can

play10:46

go

play10:47

beyond that because we're not including

play10:49

minuses so all the minuses

play10:51

are now going over to the plus side so

play10:53

we get those extra

play10:54

numbers available to us so for example

play10:57

if i go to

play10:57

uint which is down here somewhere

play11:01

then you can see oops it doesn't specify

play11:04

the range for uint's eight there oh

play11:06

there it is

play11:07

zero two two five five so now i could go

play11:11

all the way up to two five five that's

play11:13

okay if i go to two five six

play11:15

not going to work and i have to go up a

play11:17

bit size to uint

play11:18

sixteen all right so hope that all makes

play11:21

sense

play11:22

so that's integers the different sizes

play11:24

of integers and unsigned

play11:26

integers as well now most of the time

play11:28

and pretty much all the time

play11:30

for this application we'll be making

play11:31

later on we're not going to specify

play11:34

the number of bits and you probably

play11:36

won't need to most of the time unless

play11:37

you need to be

play11:38

really specific about how many bits you

play11:40

want your integer to be it needs to be a

play11:42

specific size

play11:43

we're just going to go with int for most

play11:46

of the time

play11:47

all right so the last thing i want to

play11:48

show you is a float and remember a float

play11:51

is a number which has a decimal point so

play11:53

something like 21.5

play11:55

would be a float so i'm going to say var

play11:57

score

play11:59

1 and we'll set that to be a float and

play12:01

now unlike integers we have to specify

play12:03

the bit size

play12:04

and it can be either 32 or 64. so again

play12:07

the bit size

play12:08

dictates the range of numbers we can use

play12:11

and the higher amount of bits

play12:12

the larger the range so float 32

play12:15

and it can be plus or minus so i could

play12:17

do minus 1.5 that's fine

play12:19

or i could do just plus and then

play12:21

something like 25.98

play12:24

we can also do a float 64. so far score

play12:27

one or two rather this time and that is

play12:30

going to be a float

play12:31

64 and it could be some stupidly large

play12:34

number really doesn't matter okay all

play12:37

right then so

play12:38

for the most part we'll be using float64

play12:41

because

play12:42

they have a slightly higher precision

play12:44

than float32s as well

play12:46

and to be honest it's not going to cause

play12:48

much of a memory hit

play12:49

on your computer so we'll be using

play12:52

float64 for the most time

play12:54

and in fact if you just use this

play12:57

operator right here

play12:58

whereby it's inferred the type that will

play13:00

be

play13:01

the default type so i could say for

play13:03

example

play13:04

score three colon equals

play13:07

to i don't know 1.5 that's going to be

play13:10

inferred

play13:11

as a float 64. if i hover over this

play13:14

we can see var score 3 float 64. so this

play13:18

is the kind of thing we'll be doing in

play13:20

the future all right

play13:22

and just to demo that we can't have

play13:23

decimal points on integers if i change

play13:25

this to

play13:26

20.5 you can see we get an error

play13:29

there for whole numbers only all right

play13:32

okay then so if you want to see all the

play13:34

ranges and whatnot i'm going to leave

play13:36

this link down below the video and you

play13:38

can see all the different ranges for the

play13:40

ins and the floats etc

play13:42

right here but next up in the next video

play13:45

we're going to talk a little bit more

play13:46

about this fmt

play13:47

package right here and the different

play13:49

methods we can use on that to print

play13:51

strings

play13:51

to the console

Rate This

5.0 / 5 (0 votes)

関連タグ
Go ProgrammingVariablesStringsIntegersFloatsBeginner CodingType InferenceProgramming BasicsData TypesCode Syntax
英語で要約が必要ですか?