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

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Covers variable declaration, strings, and numbers in Golang

Full Transcript

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 print 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

Original Description

Hey gang, in this Go tutorial I'll talk about how to declare variables in Go as well as look at three types in detail - strings, ints & floats. 🐱‍👤 View this course in full without ads on Net Ninja Pro: https://netninja.dev/p/learn-go-golang 🐱‍💻 Course Files: + https://github.com/iamshaunjp/golang-tutorials 🐱‍👤 JOIN THE YOUTUBE NET NINJA GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 Useful playlists: + Modern JavaScript - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc 🐱‍💻 Download Go - https://golang.org/dl/ 🐱‍💻 Go Numeric Types (bits) - https://golang.org/ref/spec#Numeric_types 🐱‍💻 Go Standard Library (bits) - https://golang.org/pkg/ 🐱‍💻 VS Code - https://code.visualstudio.com/ 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →