Golang Basic Functions - ULTIMATE Golang Basics Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's up ninjas and welcome back here to the golang dojo this channel is all about the go programming language if that sounds interesting to you make sure to give that subscribe button a big bear hug today we are covering functions in go and mostly we are going to stick with the basics of function and move a slightly more advanced forms of a function in our next video so make sure to become a subscriber for a future video here in front of us is the most basic program that anybody can write and that is a hollow program so as you can see here we have a executable program main this indicates that this is the place where everything starts every function has started with the keyword func indicating to the compiler that hey this is a function that you are reading following is the name of the function in this case the name of the function is main indicating that this is the main function where everything starts then we are going to have the brackets here then the curly bracket is wrapping around the instructions that we are going to run now font here is a built-in library and that has a function a exported function called print line and within the print line we are giving it a parameter of a type string that contains content of a hollow well partially in chinese here and if we go ahead and run this program we will get hello world besides the main function we can create other functions of our own so if we go ahead again func is the keyword that every single function uses then it would be the name of the function we are going to call it just function here don't have to be super creative and then following with the brackets and the curly brackets wrapping around the instructions that we are about to write we can go ahead and cut this and paste it here now we are just moving all of the functionalities that were originally had in our main function into this function that we newly created in turn we can call this function by writing its name and then following with the brackets indicating that hey we actually want to call this function so if we go ahead and run this program again it is we're going to do the exact same thing but now we are creating an actual layout function in order to run this instruction here now notice that a go doesn't support a function overloading meaning that if you try to have a function with the same name but maybe you have an extra parameter saying that hey i want to pass in an integer here and and then somehow print out the integer as well besides just hello world this is not going to work it is going to tell you that function has been declared in this package and you cannot declare functions of the same name even though you have different parameters so function overloading is not going to work in the go however a slightly different workaround you can do is by using the very attic function that takes in we call it periodic function that takes in multiple integers using dot dot this is kind of like a wild card indicating that you can put as many integers as you want so if we go ahead and get all of the integers from the parameter integers using the for loop here if you haven't checked out my last video on for loops make sure to check that that video out as well if this it doesn't look familiar to you we can go ahead and print out the individual integers here like this then we can go ahead and call this function in the main function uh we don't have to pass in anything because when we have this wildcard dot dot integer we can pass in anywhere from 0 to infinite amount of integers obviously you're not going to keep on putting in integers here unless you have maybe like uh integer slice or integers array for example then you can build up a data data structure and then passing that single data structure as a single parameter into a function but here you could put in one two three four five six seven eight however many you want within reasons of course otherwise your code view is not going to go through and i go ahead and print that you can see that it is opening out zero to four yes because this is actually getting the for loop is actually getting the index so if we do this instead then this is the index and this is the value of the integers so if we go ahead and print this out again now we can see that it is opening out the actual values that we are that are being passed in one two five here next uh let's talk about how we can return a result after performing some calculations in a function now we're going to create a function a different function again and this time we want to calculate the sum of a two integers being passed in we have an integer a we have another integer b and next make sure to include ends so this is going to indicate that the result that we're returning is going to be of a type ins curly brackets again and make sure you have this return keyword that following her by the results that you actually want to return in this case it is a plus or b if we go ahead and call this function sum and then pass it into integers one and two will do go ahead and run this program we would be able to see that hey nothing is getting printed out because we are actually not we're not printing out anything if we do use the print line again to print out the results of a sum we should be able to see that three being printed out here now going back to the function you can see that we are declaring saying that a is the type of end and b is tied up into twice kind of redundant so we do have some static sugar when we are using go we can just use a comma b this will indicate both of these are integers the next a couple of things that i'm going to mention are slightly more special for go the very first one is that we can actually return multiple things in a goal function now let's go ahead and create another function this time we are going to create a function that calculates the length of the name being passed in a name is going to be of a type string now here is how you can return multiple results you have a bracket inside of the bracket are going to be the different result types that you're returning the very first thing that we want to return is a string and the second result that we want to return is ins we're going to go ahead and return the name we're being passed in directly next we are going to return the length of the string name so if we go ahead and call this function as well name length passing a string wallace here run this program we should be able to see that wallets are being printed out and seven have been printed out as well which is the length of the string wallace now instead of returning actual results something that is a pretty common in go is that we can actually return errors when we don't have any results to return so it will look at something like this let's create another function called greeting taking in a name of a type string here we're going to return a string if the name is not something that we expect then we can return an error like this so if maybe the length of the name is equal to zero for example we can return an empty string back then we will return an error saying that we have a empty name and this is not good otherwise we will go ahead and return the greeting hello plus the name and the error we still need to return an error but we can go ahead and give it a nil indicating that there is no error that needs to be returned in this function let's go ahead and call the greeting function remember that we have a two results get a return and one is the actual results the other one is the potential error that we might receive greeting the parameter is going to be a string if the error is not nil then something is off we need to print out the error and see what's up otherwise we will go ahead and print out the result so in this case wallace is not a empty string so length is not going to be zero we will should be able to get a greeting with the function and the error is going to be new which means we will print out the result instead of printing out the error so let's go ahead and run this program we can indeed see that we're getting greeted however if we are passing in an empty string and the length is going to be zero then the error is going to be printed out because when the error is not new we want to print out the error go ahead and run this program again as you can see it says empty name here and this is not good now when we are getting something unexpected an error is not the only common way to handle it we can actually return something called okay so it looks something like this and let's call this a greeting okay instead so instead of returning an error we can actually return a boolean value indicating hey if there's something going on here when the length of the name is zero instead of returning an error a full-blown error we can just return hey this is not okay otherwise we will return the true so how we would use this a function is pretty similar to what we had before but instead of error we are checking if the function is getting what it's expecting so instead of checking the errors we will do if okay if it's okay we will go ahead and print out the results okay we also need to make sure we are calling the y function go ahead and run this program again we can see that the greeting is indeed again getting returned however if we have it in this empty string being passed in then we won't print anything now let's talk about recursive functions and next because their functions are just like a regular function there's nothing really special when we are writing recursive function in go we'll do the classic fibonacci function here it takes an i as a parameter and we turn an integer if i is equal to zero we will return zero else if i is equal to one then we will return one otherwise we will return fibonacci i minus 1 plus fibonacci i minus 2 recursively let's go ahead and call this function up here print out fibonacci and passing let's just do 10 go ahead and run this program again we will be able to get the recursive resolved when we are passing in 10 into the fibonacci function just for confirmation we are indeed getting the white value here now one last thing i want to mention also a very important thing to mention for go is that if we are passing in a parameter and that parameter is not of pointer type then that parameter is going to be passed by value instead of a reference or pointers or address in other languages so if we have an another function call without pointer here and it's taking a integer as a parameter and if we operate some calculations instructions on that parameter maybe i equals to i plus one the changes to this variable is not going to be reflected back into the variable and originally being passed in in order to do that we would need to take in a pointer type so change this function name to with a pointer here and go ahead and use the referencing operator on the integer pointer type here now let's go ahead and call these functions first we need to declare a integer one here go ahead and put in this variable out second we will call the function without pointer hopefully this one is not going to modify the variable being passed in thirdly we will call the with a pointer function and this one instead of passing in the variable itself we will pass in the address of the variable and this one should be making changes into the original variable being passed in so hopefully this will print out one this will print out one also but lastly it will print out two go ahead and run the program again indeed we are getting one one two so those are the basics of a functions in ego make sure to subscribe to this channel for our future video where i talk about slightly more advanced topics related to functions in go with that let's start again in a couple of days [Music]
Info
Channel: Golang Dojo
Views: 815
Rating: undefined out of 5
Keywords: golang, golang 2021, learn golang, go lang, golang in 2021, go language, golang tutorial, go tutorial, go programming language, golang tutorial for beginners, golang crash course, golang for beginners, Golang Basic Functions - ULTIMATE Golang Basics Tutorial, golang functions tutorial, golang basic functions tutorial, golang functions explained, function in go, basic functions in go, basic functions in go programming language, functions in golang, basic functions in golang
Id: 380D9mmWXl8
Channel Id: undefined
Length: 14min 28sec (868 seconds)
Published: Wed Jun 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.