Golang Tutorial for Beginners | Full Go Course
Skills:
LLM Foundations80%Tool Use & Function Calling80%LLM Engineering70%Prompt Craft60%Fine-tuning LLMs50%
Key Takeaways
This video provides a comprehensive tutorial on the Go programming language, covering its basics, syntax, and application development, with a focus on building a simple CLI application.
Full Transcript
in this full course you are going to learn about one of the youngest programming languages that is becoming more and more popular in the cloud engineering world which is go or also commonly known as go link you will learn everything you need to get started with go and start using it in your projects we're going to write a very simple cli application to learn the basics of go so instead of learning the concepts and syntax of go with isolated examples you will learn them while building an actual application throughout the tutorial so first let's see a detailed overview of what you will learn before diving right into the syntax you will first understand why go was even developed its use cases and differences to other programming languages as a next step we will set up our local development environment and see how the basic structure of a go file looks like then as we start writing our simple ticket booking application you will learn the core concepts and syntax like the most common data types such as strings integers booleans as well as arrays slices maps and struts you will also learn about variables and constants about formatted output getting and validating user inputs a concept of pointers in go as well as variable scopes then you will see how to control the application flow with loops if else and switch statements as well as how to encapsulate logic in functions and generally how to organize your code in go packages finally we will make use of go routines to parallelize some of the execution to make our application faster with concurrency so we're gonna learn a lot of things in this course and i hope you are excited to get started also i want to mention that all the demo code from this course is available in a git repository which we will link in the video description so you can follow along easily we have put a lot of effort in creating this course so if by the end of this course you think it was valuable for you let me know by leaving a like and subscribe for more content like this and also be sure to check out techworldwithnana.com for online courses and a complete devops educational program if you want to further educate yourself and bring your career to the next level now for viewers absolutely new to programming in general you can also check out my python course on youtube which is even more targeted to complete beginners in programming and with that let's get started go was created by google in 2007 and open sourced in 2009 so it's a pretty young language now whenever you hear about a new programming language the first question you probably have is why do we need yet another programming language what is the purpose of it or how is it better or different from other already existing languages so before diving into the syntax let's see what was the motivation behind creating the go language well the infrastructure where the applications were deployed changed a lot in the last few years multi-core processors became common and also using cloud infrastructure with hundreds or thousands of servers with multiple processors to deploy applications became pretty universal so infrastructure became much more scalable dynamic and had more capacity however most languages couldn't help in writing applications that would take advantage of these infrastructure improvements so basically you had applications that would just execute one task at a time in order but with infrastructure improvements it was possible to now write applications that would execute tasks in parallel do multiple things at once this way making the application faster and more user-friendly a simple example is if you are using google drive you may be uploading or downloading files and folders but you can keep navigating back and forth in the ui so downloading uploading and navigating the ui all happen in parallel or consider youtube you can be listening to this video then scroll down to the comments write a comment yourself like the video etc so the application allows you to do multiple things at once without affecting any other task and this is a concept of multi-threading so each thread is basically processing one task and you can have many of them running in parallel and this makes applications fast but may also cause some issues for example on google docs many users can work on the same document at the same time so when you have two users changing and adding stuff at the same time to the same document this should work smoothly without one user overriding all the changes that another user is making another example where such issues may happen when things are processed in parallel is a booking system for buying tickets or booking a hotel etc let's say again two users are trying to book the last remaining ticket at the same time of course this should work in a way that no double booking happens and this concept is called concurrency and needs to be handled by developers in code so they must write code that prevents any conflicts between the tasks when multiple tasks are running in parallel and updating the same data and many languages do have features for implementing such applications however the code can get pretty complex and handling and preventing the concurrency issues can be pretty hard and with complexity of course there is always a higher risk of human errors and that's where the main purpose and difference of go comes into the picture so go was designed exactly for that purpose to make writing multi-threaded concurrent applications that take advantage of the new performance infrastructure much easier and we will learn about this in more detail at the end of the course when we use go routines so logically enough the main use case of go or what it's best used for is writing applications that need to be very performant and will run on the modern scaled and distributed infrastructure with hundreds and thousands of servers typically on a cloud platform for developing go they wanted to take the simplicity of syntax of a more high-level language like python and the speed and efficiency of a lower level language like c plus plus so you will see the combination of these things in go and i have to note that go is used on the server side or the back inside of the applications and these types of applications can range from microservices and web applications to database services and so on in fact many cloud technologies or technologies that run on modern cloud environments are actually written in go like docker hashicorp vault kubernetes cockroachdb and so on now on top of this main purpose of go go actually has a lot of other advantages one of them that i already mentioned is it has a very simple syntax which makes your code very maintainable easy to read and extend another advantage is that go applications can be built very fast they are also very fast in startup and also when they're running plus go is very resource efficient which means it uses few resources or it requires fewer resources like cpu and ram to run go is also a compiled language and it compiles actually very quickly into a single binary and you can deploy it and run it on different platforms in a consistent way so no matter which operating system or which environment you're using you can take that same binary and basically use it across different platforms so considering all these benefits of simplicity and speed and so on go is becoming more and more popular for writing simple automation applications and command line interface applications for devops and sre tasks as well so now that we know why go was created and what differentiates it from other languages let's jump right into learning the core concepts and syntax of go now to start writing a go project we're gonna need two software programs first of all we're gonna need go compiler and second we're gonna need an ide or integrated development environment where we're gonna write code for go and also execute it as an id we're gonna use visual studio code which is super easy to download and install so if you don't have it yet on your computer just type in install visual studio code in google and basically on their official page you have the download options for all operating systems so go ahead download and install it so that's the first part i already have visual studio code locally so i'm gonna skip this part and second we're gonna need to install go compiler right now i don't have go locally that's why when i type go it tells me command not found so we're gonna install go which is also super easy again type install go and on their official page you have download and install guide and again for different operating systems i'm working on mac so i'm gonna download the go package for mac awesome once that's downloaded just click on the package and it will show you an installer which basically lets you install go step by step setting up everything necessary in the background so we're gonna do continue install for all users we're going to leave all the defaults install authorize and as you see installation was successful so so if i close this window we can remove the installer and go back to the terminal and type go as you see we have go installed that's why you see a bunch of go commands that you can start using now great so we have everything prepared let's create a folder in which we're gonna write our go project and i'm gonna call it booking app and using visual studio code we're going to write go program in that folder so i'm going to open visual studio code and i'm in the booking app folder you can also simply just open visual studio code and then basically choose a folder that you want to work in and you have the same result and i'm going to make it a little bigger for convenience and there is one more thing that we need to do to prepare our setup and that is installing a go extension inside the visual studio code the extension will basically help us in writing the go code it will give us a nice syntax highlighting intellisense to basically easily navigate our code help with troubleshooting and so on so extensions for different languages make it much easier to write code in that language so let's go ahead and do that to complete our setup and on the left side right here you have the extensions tab so if i click inside and then search go it will give us all the available extensions for visual studio code and the first one with the most downloads is the official one from go team at google and you also have a description of what that extension gives you and that's the extension we're going to use so click on install and that's it now we're all set up to start writing our first go application in visual studio code and for that obviously we need to create a file and this is going to be the main dot go file with dot go extension you will see a bunch of pop-ups here to install things for the go extension so go ahead and click on install all and that will install and set up the rest of the stuff and main dot go is basically a standard name for the main file where the go application code is written and to see that in the file explorer we have this booking app folder and inside we have this main.go file now how do we start writing code in go like printing a simple hello world message to the console let's do print quotes hello world and we already see the first benefit of a go extension because it tells us that this code doesn't work and as you see the error says that go can't find a file called go.mod so the first thing we need to fix is to make our go application into a project and for that we need to basically initialize it that's the error that we get here and to do that we're going to open a terminal in visual studio code new terminal window and this will give us terminal in booking app folder which we're in which is very convenient and here i'm gonna run a go command called go mod or module init and basically name of the project we called it booking app so let's use that name so this is gonna basically initiate our go application into a module or a project and we're calling it booking app so let's execute and save so what this command actually did in the background is it generated this go dot mod file which simply describes the project with the name of the project and the version of go that is used here so the first issue is now fixed now we have another error that says expected package so in go everything is organized into packages and we're going to be using go packages throughout our application and when we create our own application we also have to include it in a package and doing that is actually very simple as the first line of our go application we define keyword package and then name of the package which our application will be part of and the standard name for the main application is package main so now we have an initialized go application which is in a package called main moving on to the next error we now see again a different message that says that a declaration is expected now i have to note here that you may actually have a different sequence of errors so you might see a different message here but i'm going to show it to you with my examples so in this case we have a missing declaration and that is basically goes way of saying i don't know where to start executing this application so give me an entry point and we have to declare the entry point of our application because when we run go applications we need the main starting point for the execution so go needs to know where does it start executing the code on which line because if you have multiple files in the go project you need to give go compiler a starting point the first line of code where the execution starts and the entry point is a main function that go will look for whenever you execute your go application and we create a main function using func keyword name of the function again it has to be called main and we have to put our logic whatever we are executing within that main function so now go will know where to start executing our application and for one go application you're going to have a one main because you can only have one entry point to your application so now we have a slightly changed syntax highlighting for our print function and if we hover over it we see another error message that says undeclared name now we've come to the point where our application belongs to a package we have the entry point so go knows where to start the execution and it sees that print is the first code that it's going to execute but the problem here is that it doesn't know where print is coming from that's why we see undeclared name and print is a function that comes from a go package a built-in package called fmt or format and we have to explicitly import any packages from which we're using the functionality so we're going to do import quotes fmt that's the name of the go package and to use a print function or any function from the package we're going to do fmt dot print and immediately as you see the error is gone and as i mentioned similar to many other programming languages everything is organized into packages so the go program that we installed actually already has some built-in packages with a bunch of functionality that we can use and one of them is this package that we just imported and as i said the packages have functions that we can use so whenever we want to use a built-in go function to print a text to get user input to validate something etc we need to explicitly import it from one of the packages so think of the packages as containers of various functionalities that go gives you readily available for you to use when writing your applications now how do you know which functions are in which packages or let's say if you need to get a user input how do you know in which package you have that functionality available well you actually have to look up the documentation to see that or basically just simply google to find the right package and of course with time when using go and its packages you are going to know the most commonly used packages and all their functionalities and a cool thing we have here is you see this underline under the fmt package and if i hover over it you see a link that actually takes you to the official documentation of the fmt package so here you can actually see what functions you have available in that package and so on so with this we have our first fully functioning go application that we can now execute so let's clean up our terminal and executing a go application is very easy we have go command for it called go run and name of the file that we are executing which is main dot go so go run will basically execute this file it will look for the main function and execute the contents or lines within that function one by one we just have one line so that's getting executed and we're seeing hello world in the output we can do final optimization here to add a new line at the end of this print statement and to do that we're simply going to use a different function from format package called print ln so this will print whatever we pass here with a new line and let's execute that again and there you go now that we have the minimum program structure and know how to run go programs let's start writing our booking application logic the very first thing we need to do in our booking application is to greet our users when they visit the application so let's write some welcome message and information about the conference for which they are booking the tickets so instead of hello world we're gonna write welcome message like welcome to our conference booking application and we can write another message like get your tickets here to attend and again if we execute this we're gonna see our welcome message pretty easy now let's say our conference has a name and we're using this name in many places in our application in the welcome message in the thank you message when the user books the ticket on the ticket itself etc and whenever we have a value like conference name that is used in many places throughout the application it's the most common usage for variables every programming language has a concept of variables where instead of repeating this value everywhere you store it once in a variable assign it a name and now you can reference that value using its variable name wherever you need it in your code now if the value changes you don't have to find and update it in multiple places throughout the application code but you can simply change it in one place where you assigned it to a variable and that's it so how do we create and use variables in go well we have the value like the name of the conference let's call it go conference and we store that value in a variable and we can give that variable whatever name we want like this let's call it a name and finally we tell go that we are creating a variable using a var keyword so this basically lets us store the value go conference the conference name in a variable called name so now we can use that value by referencing the variable name wherever we need it now with variables it's a good practice to create variable names that properly describe what the value is in our case name baby two generic so to make it more specific and clear we can call it a conference name and note the camel case syntax here which is a pretty common variable naming convention in different programming languages including go and if i save those changes you see that we get a red line under our variable definition and that is something specific to go language so in go unlike many other programming languages when we define a variable or when we create a variable with a certain value and we don't use it in the code we get this error that actually says conference name declared but not used so to fix this error we actually have to use that variable and note that same error applies when you import a package but you don't use that package in your code you get the same error that package gets imported but not used which is a very good reminder to clean up your code so to fix this issue we're simply gonna print out that variable value using print line function and again when i save this change the error disappears so let's clean up the terminal output and run our go application again and there you go you see go conference printed out now let's actually use that conference name in our welcome message instead of generic our conference we're going to reference whatever we define as a conference name for that first of all let's delete the print statement and again as a common practice we're going to define the variable at the beginning of the function and we can use the variable within the print message by dividing the actual text that we have here and the variable name with commas inside the print alien function so this will be substituted by conference name and then actual text and the variable name will be separated by commas and let's save this and if i execute our go application again we will see welcome to goconference booking application so it took the value from here also note that the space automatically gets added before and after the variable value now what we also need in our application is the tickets for our conference and let's say we have total of 50 conference tickets available for users to book and for that we will also create a reference called conference tickets to store that value once and then we can reference it as many times as we want so let's call it conference tickets and the value will be 50. we have 50 tickets available now this value actually does not change throughout the application right we have 50 conference tickets and it will always stay 50. while our application is running and for such values that do not change or stay constant instead of variables we have constants now as you might think the conference name could also be constant because it also doesn't change throughout the program but for this example we will leave it as a variable so instead of var keyword here we're going to use const and that will give us the same type of reference as with variables but this time we cannot change the value of this constant but we can use it and reference it in our application exactly the same way as our variables so the const keyword tells go that this value is not allowed to change and if we actually try that and somewhere in the application code change it to some other value and then try to use that like this we get a warning here right away that says cannot assign to conference tickets because it's declared as a constant but if we change it to var and save you see that the warning disappears so let's change it back to const and clean those lines up now when users start booking the tickets obviously we need to keep track of the ticket count so when 50 users book their tickets we inform the rest that the conference is completely sold out so every time someone books a ticket we need to basically reduce the number of available tickets that users can book and for tracking that amount we're going to create another variable for remaining tickets and we're going to call it remaining tickets which again is a variable because as users book their tickets this value will get less and less and it starts at 50 because that's how many tickets we have in total and again we have this warning because we're not using it so let's actually use that in our welcome message to inform the users about how many tickets are available for the conference and how many of them are still remaining so between these two lines i'm going to add another print statement and here we're going to say we have total of this many tickets and this many are still available and if i save this the warning will go away because we are using both of these values in our code so again separating the actual text from the variable reference using commas now let's run our updated application and in the message we have the name of the conference and here it says we have total of 50 tickets and 50 are still available now whenever we're printing our text mixed with variables we can use a function called printf from the format package this function is specifically used for printing formatted data so it tells go how to format the values of the variables and generally it makes writing these kind of outputs easier so with printf this line for example will look like this so instead of print ln we're going to use printf another function from the format package and instead of the conference name reference we're going to use what's called a placeholder and annotate that using percentage sign and v and you see the syntax highlighting here that shows that it's a special character for placeholder but of course we still need to reference whatever variable we want to substitute here right and we do that right here with a comma and then conference name and if i save this and run we have the same output one thing that is different here is the new line is missing because we were using println that automatically added a new line in this case we're gonna add it explicitly using backslash n so that's a character for newline and if i save this and run we have exactly the same output as before now let's do the same for the second print statement first change it to printf take this reference here and substitute it with percentage v do the same for the second variable reference and again we need to replace so basically we need to tell go which variable values should be used to replace those placeholders and of course they should be in a correct order when we have multiple such placeholders so the first one is conference tickets the second one is remaining tickets and again the new line backslash n save it and there you go now as i mentioned printf or print format function allows you to tell go how to format the variables that you are referencing here the percentage v is the default format but you have other specific formats also available if you want the values to be displayed differently and you can see this whole list in the gold documentation under the fmt package link so the percentage v is the one we used we have a lot of other options as well for numbers and text values and so on now sometimes when we create a variable we don't immediately know its value for example when we're getting user input like a user entering their name and date of birth etc in the application we don't know that value before because we don't know what the user is going to enter so we create a variable and later we assign it the value which is possible to do in go just like in other programming languages so let's see the syntax for that so here let's say we define a variable called username and we don't know what the value of that variable is going to be and on the next line we ask the user for their name and this is a syntax for comment so whatever starts with double forward slash signs is basically interpreted as a comment so it's not executed as code so let's say we have some imaginary code here that asks for user input and they and that user name that we get is let's say tom and finally we can actually use that value so we have defined a variable here and then we are assigning a value for that variable later on a separate line however when i save this code you see that we get another warning here that says unexpected newline expecting type so what's the problem here and why is it expecting a type well in go all values have data types and generally in any programming language you have multiple data types for different use cases and go isn't an exception the difference between these languages is however which data types exactly they support so each language has its own set of data types two most basic and common data types are strings and integers for textual data like the welcome message the name of the conference so anything between the quotes basically is a string data type for numeric data like ticket count h and so on we have integer data type and again you see the syntax highlighting for the integers and strings are different because go basically knows this is a string this is an integer so these are the two most basic ones but we're going to learn a few other data types as we go along here however it's important to understand that each data type can be used differently and behaves differently for example you can do calculations with integers but you can't do calculations with strings you can chain or get a subset of a string but you can do that with integers so the main purpose of having types for values in code is to avoid accidentally using an integer as a string or vice versa and use one data type instead of the other which may break your application so when we create variables in go it needs a type now you may be thinking when we created these two variables right here these two variables and a constant we didn't specify a type so why didn't we get the same syntax error here well when we create a variable or constant and assign a value to it immediately on the same line go can imply the data type based on the value so go knows that this is a variable type for string and this is a constant and variable types for integers because of the values but when we do not assign a value immediately go doesn't know what type of value you are going to store here so it asks you to explicitly define a type to make your code basically more robust and prevent you or other developers from accidentally assigning a wrong data type value to that variable later in the code so in this case we need to define a type explicitly how do we define a type super easy at the end we just say this is a variable of string type if i save this the error disappears and again you see the syntax highlighting for the type and the same way we can define a variable for integer type let's say user tickets which is a number of tickets user wants to buy and this is going to be an integer type and somewhere here we're going to ask user for the input and let's say they enter 2 and in the print statement with printf we're going to say user whatever their username booked this many tickets with a new line and reference the respective variables and again let's run this and here we have our output now you probably already noticed something really cool about go especially if you have worked with other programming languages before and that is that while we're writing this code if we make some mistakes if we have errors in our code for example defining a variable and not using it or forgetting to specify a type etc go basically detects those errors before we even run the application so by the time we're ready to execute the application we have fixed a bunch of errors that go identified and highlighted for us in many programming languages that's not the case usually you discover these kind of errors when you run the application not while you're coding and that is a big advantage of go now going back to the types i mentioned that go actually implies the types of these three values automatically and if we wanted we could actually print out the types of variables using the printf function so with fmt printf let's print out the types of these two variables and a constant so conference tickets is whatever type it has then remaining tickets is whatever type this one has and then conference name is percentage t percentage t is a placeholder for the type of the variable that we're referencing here not the value like percentage v but the type so let's pass in those references and save and if we run our application we should see the output here conference tickets is int or integer remaining tickets is in conference name is string so this lets you print type of any variable now if we wanted we could actually define the types explicitly here even though it's not required so we could do conference name as a string and int and int however specifying a type explicitly when go can detect it makes sense when want to specify a different type than what go would detect for example in go specifically again we have multiple times for numeric values so in addition to int or integer type which represents whole numbers we have int 8 in 16 and 32 and int 64 which corresponds to the length of the integer so basically how big or how large the number is and in addition to that we also have uiint or unsigned integer which represents whole numbers like integer but only positive ones so 0 and plus and here's a chart of all the different numeric data types in go and obviously each one has its own purpose so the question is when to use which one and why do we have so many different integer data types for example well this allows you to define data types in a way that the values will automatically be validated so if you're using uiint and assign a negative value to it you will get an error like in our case the number of remaining tickets should never be negative but with int type it can be so somewhere in the code we can actually set it to a negative number like this but if we specify a type uint explicitly instead and save it you see that it will not accept the negative value so setting a type may actually protect our variable from getting a value that it's not supposed to get and obviously you as a developer are not going to assign your variables a wrong value but if you're doing some calculations on your variables or some processing the result of that calculation may be a wrong value we also have float number type which are for numbers with fractional parts so not whole numbers this could be statistical data like a conference attendance compared to last year for example this could also be monetary values like prices of products in an online shop or transactions in an online banking application and so on so basically when a number needs a higher precision float number type needs to be used but as i said there are other data types in go besides textual and numeric types which we will learn as well now there is one more thing in terms of variable definition in go and that is again specific to go is that we have an alternative syntax for creating a variable and assigning it a value directly this is like a syntactic sugar of go language so instead of this syntax for creating a variable we can get rid of the var keyword as well as the type and right here before the equal sign just add a column and this will do the same as before create a variable and assign it a value now note here that with this alternative syntax you cannot declare constants so we cannot do this with constants it only applies to variables and it also doesn't work if you want to explicitly define a type for your variable like here for example now let's clean up our code and let's go back to our ticket booking logic so this is a booking application so want to allow users to book their tickets and for that we want to ask users for their personal information first like first name last name etc and all these values will be then saved into variables like this so we need some logic here that allows the application to ask for user input in order to read user input we use another function from the same format package called skin so basically the format package gives us different functions to print out like this formatted output or read formatted input so it's used for input output processing functionality so let's scan the user input for their first name because instead of assigning the value directly as we did here we want to get that value from the user and then assign it to the username variable now print function like printf or println takes a message as a parameter like this one a formatted message and prints it scan also takes a parameter which is the input it needs to scan and the parameter for scan function is the user input but we don't know what the user will enter as their name so we need a way to save the user input as a variable and then reference that value using that variable name instead of directly assigning it here and let's save it so this is supposed to scan user input and whatever user enters basically assign that value to username variable as a value now if we run this application let's run it you see that it runs it executes all those lines and it just exits it doesn't wait for any user input so we were not able to enter anything and the value of the user here or username variable is also empty so there is one thing that we need to fix here so before the username variable we need to add what's called a pointer like this so what is a pointer simply explained i said that we save a value in a variable in order to use it later so when we create a variable where does that value actually get stored well values are saved in memory on your computer so that 50 or go conference values are actually stored in memory but whenever we reference that value using the variable name we defined go compiler must go and find that value in memory so it needs to know where in memory exactly it is stored or in other words it needs to know the memory address of that value and a pointer is a variable that points to the memory address of another variable that references the actual value 50 for example and pointers in golang are also called special variables so to see that in the code let's actually comment this out and if we print out any variable like remaining tickets for example with value 50 this will give us the actual value 50 but if we print out the pointer this will actually print out the memory location of the remaining tickets variable let's actually see that right here we have value 50 and the next line is a hash which is a memory address for the remaining tickets variable so that's basically a pointer and it's also important to mention that pointers is a concept used in the c programming language but many popular programming languages like java or javascript for example to not have pointers at least not exposed to you as a developer so you will not see the concept of pointers in many other languages so again going back to our scan function instead of passing the value of the username variable which is empty we're passing the memory address of that variable so that scan function will read whatever the user enters and assign that value to that username variable in memory so if i execute the application now you see that it doesn't just exit it actually stops at this line after get your tickets here to attend and it's now waiting for me as a user to input some data and as a username i can input my own name and if i enter it goes to the next line and it prints out whatever is in the username booked to tickets so now we have that value whatever user entered available in our code through this username variable now to make this a little bit user-friendlier we can also ask the user explicitly what we are asking for so we can say enter your first name like this and let's run again and there you go so now we are actually asking the user explicitly what we want and then printing the result out so that's one value now let's read other user data like last name email and number of tickets user wants to book and let's first call this first name and update it everywhere like this let's create last name which is also string and email which is also string and user tickets which we already have which is an int and now one by one let's actually scan all those values i'm going to copy these two lines and we're going to say enter your last name and we're going to save that value that user enters in a last name variable again let's copy and let's say enter your email address and the value will be stored in email variable and finally instead of assigning user tickets directly we're going to ask the user how many tickets they want enter number of tickets and again this will be stored in user tickets variable and at the end let's actually print out some kind of a thank you message to the user with some additional information so let's write thank you first name last name of the user for booking so many tickets you will receive a confirmation email at whatever email address they gave us and don't forget the new line at the end and now let's actually substitute those placeholders with the actual variable references so we have first name last name and here we have the third one user tickets and finally the email address they gave us awesome so let's clean this up and run our program enter your first name nana enter your last name my last name email address something like this and finally number of tickets let's say three tickets and if i enter this is the last skin so now it's gonna execute the print statement that says thank you nashia for booking three tickets you will receive a confirmation email at this email address now we're getting user input but no tickets are being booked the remaining tickets doesn't get reduced it always stays 50. so let's write some simple logic to book the ticket which in our case will be just reducing the number of remaining tickets so after getting user info we will simply add remaining tickets minus user tickets so this will give us some value 50 minus how many tickets user booked and then we have to save it back into the remaining tickets variable to update it right because we have to update the value of this variable by assigning it back now when i save it we see an issue and that is type mismatch of uiint and int because we're doing a calculation on two numbers which have different types one of them is you integer and another one user tickets is an integer so as you see in goal when you do calculations between numbers they have to have the same type one way to handle this issue in many languages is to convert one of them to the other type using various built-in functions that you have available in the language a simple solution for us is to make the user tickets also you in type because users can only book positive number of tickets right they can't book minus one or minus two number of tickets so let's make it you int as well and now the error is gone so that's our super simple booking logic and after the thank you message we're also going to print information about how many tickets are now remaining so let's say this many tickets remaining for this conference remaining tickets and conference name so with these changes let's now run our go application and again enter the user values and let's say we're buying 15 tickets and enter and as you see the remaining tickets are now 35 50 minus 15 that we entered before moving on i want to give a shout out to castin who made this video possible kessen's k10 is the data management platform for kubernetes k10 basically takes off most of the load of doing backup and restore in kubernetes from the cluster administrators it has a very simple ui so it's super easy to work with and has an intelligent logic which does all the heavy lifting for you and with my link you can download k10 for free and get 10 nodes free forever to do your kubernetes backups so make sure to check out the link in the video description and now let's continue now we're saving user data in variables and are booking the ticket for them but when multiple users book the tickets we need to save all these user data in some kind of a list right to keep track of who is attending the event and who booked the tickets and for that we have data types called arrays and slices arrays and slices are commonly used data types in go applications so let's create an array for all the bookings so right here i'm going to create a variable called bookings and this is going to be an array when we define an array the first thing we need is the size because arrays in go have fixed size and for that we use square brackets and within those square brackets we define the length or size of an array and let's say we expect maximum 50 bookings so we're gonna say 50. so the size of an array is basically how many elements can this array hold so as i said array is like a list of bookings or list of elements and with the size 50 we're saying this can have 50 elements in that list the next thing we need to define is the type of the elements this array will contain a data type so what kind of values are we going to store in that is it going to be a list of integers a list of strings etc in our case let's say we want to store a list of names of the users who book the tickets a name is a string so our array is going to be an array of strings so that's the data type and finally we need the actual value right so this is the same syntax as this one right here where we are assigning a value directly to the variable so we need to actually assign a value of an array and we can have an empty array like this or we can already put some elements in the array and let's say these are some of the names inside and this gives us a bookings array with size 50 with three elements already in that array now there are two things i want to mention here first of all because we have to define the type of the elements for the array we can't mix any other type here so we cannot have names and then an integer for example right this will not work and again this is specific to go you can actually mix the types in some other programming languages the second thing i want to mention is that even though we are starting with three elements here we can actually update our array and add new elements up to 50 as well as remove the elements so again this is a variable so we can add new elements to the list and remove that during the application execution and usually when you create an array you actually start with an empty list like this because you don't know the values at this point and as the program executes you basically add new values one by one so that's going to be our starting point and because we are creating an empty array an alternative syntax for this will actually be to define the array variable like this without assigning a value and going with the default empty array but as you learned in this case we have to define the type for the array because go doesn't know the type and again you see here it's expecting type so what is an array type in go well it's actually a combination of the size that we defined which is 50 and the type of the elements it's going to contain like this so this is actually an array type and we're getting this error because we're declaring a variable but not using it so let's actually use it and let's start by adding new elements to that array how can we do that pretty simple actually we have a syntax with indexes so bookings array with index which is the position in which we're adding a new value like this so again our array has 50 empty positions where we can add values and we're saying the position 0 which is the first position let's assign this value and we can say position let's say 10 let's assign a different value but of course normally you would go one by one you would add the next value in the list like this now in our case again we want to add a name of the user that booked the ticket we want to save the first name and the last name of the user so instead of assigning a value directly we're going to say first name and add some space in between and the last name so this will give us first name last name with space separated however as you see here we get an error that first name and last name are not defined because we're using these variables before they are declared here again in a language like javascript for example the order doesn't matter so this would work in javascript but in go you have to use the variable after it has been defined in the code so we're gonna take this assignment and basically move it down here under the logic for updating the remaining tickets and if i save this the error is gone so to wrap this up we're creating an array variable at the beginning which is defined as an array of strings with 50 elements maximum and right here after user has entered their first name last name and booked the ticket we are adding that user's full name to the bookings variable and to also see what's inside the bookings let's actually print out the contents of the array and also some information about the array to see how it actually looks like so let's do fmt print f and first let's print out the whole array placeholder v and newline and this is going to be whole bookings array then let's print the first value which is bookings at index 0 and we need to use capital p here like this then let's also print the type of the array like this and finally let's print the size or length of the array and to get the size of an array we have a built-in function called length with l e n that takes the array as a input so to say and gives us the size of the array and here we need percentage and capital t and now let's actually run our application and see how arrays work book five tickets and here you see all the array information so first of all we have the whole array that has one element which is first name last name of my user and you see that it has square brackets at the beginning and here at the end so basically this represents the space for all the other elements in the array because we have a fixed size of 50 elements and that's why you have a space here then we have just the first value which is this one right here without the brackets the array type is what we actually defined here as a type and then we have array length which is 50. now what happens if we try to add value to this array at an index 52 for example so basically index outside the range of the array size let's try to do that so instead of zero index we're going to do 52. again without running the application go actually detected that your index is out of range or out of bounds and you need to fix that again if you have some calculated number here that you don't know refers to a wrong index go will help you basicall
Original Description
Full Golang Tutorial to learn the Go Programming Language while building a simple CLI application
In this full Golang course you will learn about one of the youngest programming languages that is becoming more and more popular in the cloud engineering world, which is Go or also commonly known as Golang.
You will learn everything you need to get started with Go and start using it in your projects. We're going to write a very simple CLI application to learn the basics of Go. So instead of learning the concepts and syntax of Go with isolated examples, you will learn them while building an actual application throughout the tutorial.
Before diving right into the syntax you will first understand why Go was even developed, its use cases or what Golang is used for and how Go is different from other programming languages.
#golang #techworldwithnana
🚀 6-month DevOps Certification program to become a DevOps professional: https://bit.ly/4gCporh
▬▬▬▬▬▬ L I N K S 🔗▬▬▬▬▬▬
Git Repo: ► https://gitlab.com/nanuchi/go-full-course-youtube
► This video is sponsored by Kasten 🙌🏼
► Free Kubernetes Backup and Migration - Download Kasten's K10 and Get 10 nodes free forever: https://www.kasten.io/nana
▬▬▬▬▬▬ T I M E S T A M P S ⏰ ▬▬▬▬▬▬
0:00 - Intro & Course Overview
INTRODUCTION TO GO
02:47 - What is Go? Why Go? How it's different?
06:50 - Characteristics of Go and Go Use Cases
GO SYNTAX & CONCEPTS
08:59 - Local Setup - Install Go & Editor
12:54 - Write our First Program & Structure of a Go File
22:02 - Variables & Constants in Go
30:43 - Formatted Output - printf
33:43 - Data Types in Go
45:18 - Getting User Input
47:19 - What is a Pointer?
53:55 - Book Ticket Logic
57:16 - Arrays & Slices
1:11:12 - Loops in Go
1:24:24 - Conditionals (if / else) and Boolean Data Type
1:39:33 - Validate User Input
1:54:02 - Switch Statement
1:58:37 - Encapsulate Logic with Functions
2:22:36 - Organize Code with Go Packages
2:35:39 - S
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from TechWorld with Nana · TechWorld with Nana · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
What is Docker? Docker container concept explained || Docker Tutorial 1
TechWorld with Nana
What is a Docker Container? Docker Demo || Docker Tutorial 2
TechWorld with Nana
How to install docker? Step by Step || Docker Tutorial 3
TechWorld with Nana
8 Basic Docker Commands || Docker Tutorial 4
TechWorld with Nana
Debugging Docker Containers with docker exec and docker logs || Docker Tutorial 5
TechWorld with Nana
Docker vs Virtual Machine | simply explained || Docker Tutorial 6
TechWorld with Nana
Overview of Workflow with Docker - Docker in Practice || Docker Tutorial 7
TechWorld with Nana
Developing with Docker - Docker in Practice || Docker Tutorial 8
TechWorld with Nana
Docker Compose Tutorial - Docker in Practice || Docker Tutorial 9
TechWorld with Nana
Dockerfile Tutorial - Docker in Practice || Docker Tutorial 10
TechWorld with Nana
Private Repository explained | Registry on AWS - Docker in Practice || Docker Tutorial 11
TechWorld with Nana
Docker Volumes explained in 6 minutes
TechWorld with Nana
Deploying the containerized application with Docker Compose || Docker Tutorial 12
TechWorld with Nana
Docker Volumes Demo || Docker Tutorial 13
TechWorld with Nana
Docker vs Kubernetes vs Docker Swarm | Comparison in 5 mins
TechWorld with Nana
What is Kubernetes | Kubernetes explained in 15 mins
TechWorld with Nana
Kubernetes Components explained! Pods, Services, Secrets, ConfigMap | Kubernetes Tutorial 14
TechWorld with Nana
Kubernetes Architecture explained | Kubernetes Tutorial 15
TechWorld with Nana
Benefits of Kubernetes | Scalability, High Availability, Disaster Recovery | Kubernetes Tutorial 16
TechWorld with Nana
Minikube and Kubectl explained | Setup for Beginners | Kubernetes Tutorial 17
TechWorld with Nana
Top 3 programming languages to learn in 2020 | meta analysis
TechWorld with Nana
Kubectl Basic Commands - Create and Debug Pod in a Minikube cluster | Kubernetes Tutorial 18
TechWorld with Nana
Kubernetes YAML File Explained - Deployment and Service | Kubernetes Tutorial 19
TechWorld with Nana
Run Jenkins in Docker Container - Jenkins Pipeline Tutorial for Beginners 1/4
TechWorld with Nana
Create Multibranch Pipeline with Git - Jenkins Pipeline Tutorial for Beginners 2/4
TechWorld with Nana
Jenkinsfile - Jenkins Pipeline Tutorial for Beginners 3/4
TechWorld with Nana
Trigger Jenkins Build automatically - Jenkins Pipeline Tutorial for Beginners 4/4
TechWorld with Nana
Complete Application Deployment using Kubernetes Components | Kubernetes Tutorial 20
TechWorld with Nana
Kubernetes Namespaces Explained in 15 mins | Kubernetes Tutorial 21
TechWorld with Nana
Configure Build Tools in Jenkins and Jenkinsfile | Jenkins Tutorial
TechWorld with Nana
Complete Jenkins Pipeline Tutorial | Jenkinsfile explained
TechWorld with Nana
Kubernetes Ingress Tutorial for Beginners | simply explained | Kubernetes Tutorial 22
TechWorld with Nana
What is Helm in Kubernetes? Helm and Helm Charts explained | Kubernetes Tutorial 23
TechWorld with Nana
How Websites Work | simply explained with examples
TechWorld with Nana
What is JavaScript? | JavaScript Tutorial #1
TechWorld with Nana
What is Ansible | Ansible Playbook explained | Ansible Tutorial for Beginners
TechWorld with Nana
JavaScript Variables & JavaScript Data Types explained | JavaScript Tutorial #2
TechWorld with Nana
How Prometheus Monitoring works | Prometheus Architecture explained
TechWorld with Nana
Where to write JavaScript | Where to execute JavaScript Code | JavaScript Tutorial #3
TechWorld with Nana
JavaScript Operators & JavaScript Conditionals | JavaScript Tutorial #4
TechWorld with Nana
Pods and Containers - Kubernetes Networking | Container Communication inside the Pod
TechWorld with Nana
Kubernetes Volumes explained | Persistent Volume, Persistent Volume Claim & Storage Class
TechWorld with Nana
Kubernetes ConfigMap and Secret as Kubernetes Volumes | Demo
TechWorld with Nana
Pull Image from Private Docker Registry in Kubernetes cluster | Demo
TechWorld with Nana
Kubernetes StatefulSet simply explained | Deployment vs StatefulSet
TechWorld with Nana
Yaml Tutorial | Learn YAML in 18 mins
TechWorld with Nana
Terraform explained in 15 mins | Terraform Tutorial for Beginners
TechWorld with Nana
Setup Prometheus Monitoring on Kubernetes using Helm and Prometheus Operator | Part 1
TechWorld with Nana
Managed Kubernetes Cluster explained | Kubernetes on Cloud (1/2)
TechWorld with Nana
Step by Step Application Deployment on LKE using Helm | Kubernetes on Cloud (2/2)
TechWorld with Nana
Kubernetes Operator simply explained in 10 mins
TechWorld with Nana
What is Infrastructure as Code? Difference of Infrastructure as Code Tools
TechWorld with Nana
AWS EKS - Create Kubernetes cluster on Amazon EKS | the easy way
TechWorld with Nana
Prometheus Monitoring - Steps to monitor third-party apps using Prometheus Exporter | Part 2
TechWorld with Nana
GitHub Actions Tutorial - Basic Concepts and CI/CD Pipeline with Docker
TechWorld with Nana
Docker Tutorial for Beginners [FULL COURSE in 3 Hours]
TechWorld with Nana
Kubernetes Services explained | ClusterIP vs NodePort vs LoadBalancer vs Headless Service
TechWorld with Nana
Kubernetes Tutorial for Beginners [FULL COURSE in 4 Hours]
TechWorld with Nana
Containers on AWS Overview: ECS | EKS | Fargate | ECR
TechWorld with Nana
Kubernetes is dropping Docker support - What does it mean for YOU?
TechWorld with Nana
More on: LLM Foundations
View skill →Related Reads
📰
📰
📰
📰
Groovy Open source tools Worth Your Weekend
Medium · DevOps
🚀 MyZubster is Live! From Zero to Production on a VPS
Dev.to AI
MCP Ecosystem Week 30: When Your Developers' AI Tools Connect to Everything—What's in Your Allowlist?
Dev.to AI
Receipts, not labels: what cron trust hand-offs get wrong about provenance
Dev.to · Aloya
Chapters (18)
Intro & Course Overview
2:47
What is Go? Why Go? How it's different?
6:50
Characteristics of Go and Go Use Cases
8:59
Local Setup - Install Go & Editor
12:54
Write our First Program & Structure of a Go File
22:02
Variables & Constants in Go
30:43
Formatted Output - printf
33:43
Data Types in Go
45:18
Getting User Input
47:19
What is a Pointer?
53:55
Book Ticket Logic
57:16
Arrays & Slices
1:11:12
Loops in Go
1:24:24
Conditionals (if / else) and Boolean Data Type
1:39:33
Validate User Input
1:54:02
Switch Statement
1:58:37
Encapsulate Logic with Functions
2:22:36
Organize Code with Go Packages
🎓
Tutor Explanation
DeepCamp AI