Golang Advanced Functions - ULTIMATE Golang Basics Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] some ninjas welcome back to the goliath dojo and this channel is all about the girl programming language if that sounds interesting to you make sure to give it that subscribe button a first blood today's video is going to be a extension of our last tutorial video on the basics of functions today we are going to go over slightly more advanced things that you can do with the functions in go now the very first thing that we want to learn about functions is the functions are first a class of values in the go if we go ahead and create a function we call return a parameter a certain program and have integer i as a parameter and an integer as a return type and then go ahead and return i as the result and then we can pretty much use this function just like we would with any other variable types in the go just like integers or arrays or maps that said we can go ahead and print out the type of the function just like we would putting out the type of integer a map in an array go ahead and run this program as you can see we are printing out the type of this function it takes in an integer and returns an integer as end result now when we are inspecting the type of this function we are not calling the function itself if that's what we want to do we need to give it a couple of parentheses and in this case in particular we need to give it a parameter we'll just put one here and i call this function again as you can see it is returning a type integer here and it makes sense because this is the return type of the function of return param now just like how you would pass integers in this case into a function you can absolutely pass a function into a function so if we go ahead and copy and paste this and return the function instead not only can we pass in a function as a parameter we can also return a function as a result of this function this function parameter is named f and it is a function that takes in an integer and returning integer as the result and inside of this function we can go ahead and return the function and having the i as the parameter being passed into this function so if we go ahead and call this function as well again pass in the function as the parameter go ahead and run this program as you can see it is returning an integer as a result type if we want to make it a little bit more clear then we can go ahead and print out the result of this function which is going to be 1. and it makes sense because we are passing in a parameter integer that is one and this function which is this and this function is going to return whatever the parameter being passed in which is going to be one and as you can see the result here is indeed one so far it's been a pretty straightforward we are defining a function here and then calling the function here however what if we don't actually want to call these functions in order we can go ahead and utilize the deferred keyword indigo what do i mean by that and what how does it really work so let's go ahead and declare two functions in particular here the very first one is going to be a first function and this function that doesn't really do anything besides just calling first or printing out first and the second function that i want to create is going to be a second function and this function does nothing but just printing out second here now if we go ahead and call these two functions here and run this program we can see that first and the second have been being printed out in order but that's not really what we want right like we briefly discussed what we want is we want to use the default keyword uh to defer the first function so that it is not going to be called in order let's go ahead and run this program again as you can see the first function or the instruction of printing out first is it being a deferred towards at the very end of the main function now if we go ahead and call this second the second time the reason i want to do that is to show you that the deferred keyword doesn't defer it to the end of the second function immediately called after the first function but towards at the very end of the function or towards the very end of the the main function here so if we go ahead and run this program as you can see the first word is being printed out at the end of a main rather than i just defer it one function afterwards so this is particularly good for things like closing files or timing latencies when you are building a little bit more production-ready code but when you are just working on side projects it's probably going to be pretty solid in the case that you're going to use a defer for that so we are going to go ahead and comment this out so that a lot of the things that we'll be putting out moving forward in this tutorial is not going to be as confusing another functionality you are going to see in functions indigo commonly is anonymous of functions so that's what we're going to do we will declare a anonymous function like this this is anonymous a function and it wouldn't do anything besides such as printing out hey this is a anonymous function if we go ahead and print out this a function here or call this function here you will see that the function is of course running this line of instruction but what i want to point out is that if we go ahead and have another line of instruction to print out let's say hello world here and run this program again as you can see hello world is actually going to be printed out before a function anonymous function is being called here because when we are declaring or defining the anonymous function it's not when there is anonymous function is being called but rather when we actually give it a couple of parentheses here this is when the function is being called now you might be thinking this anonymous function is being assigned to a variable it doesn't sound very anonymous right for that we can go ahead and not have to assign this anonymous function into anything however we still need to have a way to call this function otherwise there's no point declaring a function that we that we're not going to call in order to do that we can give it a couple parentheses y off the bat so we can say this is anonymous function too go ahead and run this program again as you can see it is being called just fine now remember i said functions are first a class of values in a go so you can just use it just like how you would use integers or maps or arrays for that reason we can actually we assign functions to a variable so as you can see here we have already declared a a function a variable that is a anonymous function we can go ahead and reassign it to a different function instead we can just reassign it to first here and then call this function again run this program again as you can see the first function is indeed being assigned to a function here when the a function is being called matter of fact the first function is being called there's a one instruction in the first function and that is putting out the word first and that is what is doing right here notice that this reassignment only works because first and the original function being assigned to a function are of the same type so this function is just a simple function that doesn't take in anything it doesn't return anything but if we were to assign it with a return parameter that takes in a an integer as a parameter and returns an integer as a result this reassignment wouldn't work because golang is a statically typed language we cannot reassign a different type to a variable after that variable has already declared its own type for that reason we are going to revert this back to what we had before now keep in mind that all of these things that we just talked about can be as crazy as you are willing to make it to be so let's explore some ideas here let's go ahead and declare a function here called crazy function and we're going to make it crazy so this crazy function is going to take in a a parameter f and there's a parameter f is of a type function which takes in a function as a parameter and return a function as a result and this a crazy function we're taking this as a parameter and return a type that is a function and this return type function will have a parameter as a parameter and returning a function as a result then we're going to return f as the result of this equation function here now in order to call this crazy function here we need to create another crazy edge a function as a parameter being passed in here so we go ahead and do that there's a crazy edge function will be an anonymous function of type this we need to have this equation h a function b of type function that things in that takes in a function and returning a function as a result so we go ahead and return the function here we give it a this parameter a name so this function will return the result of this a crazy is a function here so go ahead and put this as the parameter uh for the crazy function here and after that we need to give this a crazy function we actually need to call this crazy function the result of the quasi function being returned here as you can see we need this crazy function results it takes in a function as a parameter so it's just a regular function at this point even though everything prior to that is not we go ahead and put first as the parameter of the results being passed in and i go ahead and call this function because it will return a function as a result of the parameter being passed in and this is how you would call this crazy function here and go ahead and run this program and as you can see we can successfully call the function being passed into this crazy function which is this first function here which is calling this instruction running this instruction of printing out the word first here that's it for today's video if i made it more confusing than before you came to this video in order to find clarity of slightly more advanced functions then i've done my job pretty quickly alright so make sure to subscribe to this channel if you found this video entertaining at least if it's not helpful and i'll see you guys next time [Music] you
Info
Channel: Golang Dojo
Views: 1,757
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 Advanced Functions - ULTIMATE Golang Basics Tutorial, golang functions tutorial, golang advanced functions tutorial, golang advanced functions explained, function in go, advanced functions in go, advanced functions in go programming language, advanced functions in golang
Id: aaO5n9QxRjg
Channel Id: undefined
Length: 12min 33sec (753 seconds)
Published: Sat Jun 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.