GoLang: 10+ UNIQUE Concepts/Conventions that Beginners Should Know About!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right what is going on guys so today today I wanted to make a little something for those who are newer to go and I wanted to break down 10 Concepts and conventions that are unique to go so these are sort of things that you're gonna find in go but not in a lot of other languages and there's sort of there are a lot of things that set the language apart they're Core Concepts that will come up again and again so before I get into that make sure that you guys are subscribed I'm gonna have new go content coming out every single day all the way through February 28th of this year and without further Ado let's start breaking down these 10 Concepts number one is going to be convention and that convention is that all package names should be one lowercase word some people do this differently and there's different things but this is straight from the goes documentation the idiomatic go guide written by the go developers themselves they recommend that all of your packages are named with one word all lower case and if you deal with any collisions anywhere you can simply change the import name on there but you probably won't run into any collisions for a long time number two is gonna be our second and our last convention and that is going to be that variable name should be mixed case or snake case however you want to call it which means that you are going to be alternating your the names within your variables by capitalization instead of an underscore so this is snake case so snake case is not the preferred convention in go it's actually camel casing so make sure you're doing that that's what you're going to see in most big library for number three we're gonna start getting into Concepts and the first concept I want to talk about is that that switch statements and if statements can actually take initializations as part of their syntax so what we can do in here is we can take our if statement right here we can say if error colon equals Foos that means that we're calling the foo function we're assigning it equal to error and then afterwards we're putting a semicolon on here and then we're doing error not equal to nil and then we're checking it in here so this error not equal to nil is going to take the value out of this so if I ran this function if I do go run main.go we're going to get out Foo error because we returned an error here but if I just went ahead I returned nil we would get an eye return nil let me get go run main.go we're gonna get nothing this will be taking the error value out of this function and then I'll be printing it out here if it matches this condition so it's a super way to shorthand if you have like a database call or something like that where you need to pull out an error or a value you can check and then do it all in one line makes the syntax a lot cleaner and nicer and something you're going to see so you should definitely know it exists for number four we're going to be talking about reassignments using the colon equals operator so if you're familiar with go at all you know that we have this colon equals operator which will allow us to declare a new variable now with this colon equals operator we can't redeclare something so if I went up here and I said c colon equals zero I did C colon equals one this is going to yell at me and it's not going to let me to do this because it says no new variables on the left side of colon equals but I can do this when I have error declared up here so this is the first time I'm initializing and declaring error and then down here with B I'm technically in the syntax I'm redeclaring error but go is smart enough to know that I'm not redeclaring it but I'm simply changing the value of it so as long as you have at least one new value on the left side of the colon equals you can go ahead and use as many old variables as you want and reassign sign them on with a call and equal statement this is especially useful when you get into these sort of if error not equal to nil change where you have to do a bunch of calls to a bunch of functions and then you're going to get a bunch of errors out and you can go ahead and just check all of these here you don't have to reassign error or make a new error name every time makes it really easy and clean to deal with number five is going to be the for Loop so in go one of the unique things is there is no while loop if you come from any other language you're probably used to having a while loop a do while loop a for Loop before each Loop all these things go only has a for Loop So within go we can use the for Loop in many different ways and we can use it like any of these other loops and all these other languages you just have to change what you're passing in so if we're passing in so if we're passing in multiple operators separated by a semicolon this is going to act like a traditional C type for Loop where we initialize some of it where we initialize a value here we set a condition right here and then we do something here to change our to update the value within the loop so the traditional example would be i setting I equal to zero looping until it's less than some value whatever but we can also do the same thing that you would get with a while loop over here so we can do 4 for a condition so you just have a condition in here instead of having to do all this stuff by just declaring it like this and then another thing that we can even do is if we went down here we did four we did four and then passed nothing in this is actually going to go ahead and run infinitely so this will create an infinite Loop and then right here I'm adding this break statement to actually break us out of the loop so we can use a break statement continue statement whatever those are similar to other languages but the key thing here is the for Loop is what will the four is what will run all of your Loops instead of having a while a do while ETC number six is going to be multiple returns go is unique in that it uses multiple returns and this is sort of typically how it handles its errors and the way it does this is every method can potentially every function can potentially return one or more different values so we could have this Foo function up here which right now obviously is doing nothing of note but you could have it actually doing something of note and right here I'm just having it return zero and nil and that means that when I come out here and I do full uh when I'm getting the values out of Foo I can assign an integer I and I can assign an error error and then I can read based off whatever happens in here here the within the go documentation they describe this as trying to get rid of like some of the C anti-patterns of where you have to return like negative one or one or a status code from a function that doesn't have any that wouldn't normally have a return or something like that um some people are a lot of people are very split on this but personally I like it and it makes it very uh clean and easy to write your functions handle your errors you just pass them up out of the function and you handle them here so that's a very key thing you need to know within go functions can have multiple returns and if you have multiple returns you need to return for all of those I can't just go in here and do return one and then nothing it will yell at me here and it'll say you need not enough return values that need the error over here number seven is the defarkee word the defer keyword is something that not a lot of languages have but it's one of my favorite things about go and it's really subtle but it makes a lot of sense once you start using it and it makes managing stuff like database connections or a mutex or something like that super clean and simple to do so what a defer what the defer keyword will do is it will take any function we have and then we'll run this at the end of the it will run whatever we pass pass into the defer function when its parent function returns so in this example right here I have up at the top here I have defer Funk I'm just making an anonymous function here you could have a name function or import a function or whatever you want to do so you just defer some function and what I'm going to do in here is I'm going to say end of loop so this is technically running at the top you would expect in any other language This would run at the very start of this function but in go this is actually going to run at the end because it's deferred so what you're going to see is you're going to see 0 through 9 printing out down here then we'll return and when we return we'll actually call this function so I do go run main.go you can see we have one through nine being printed or zero through nine being printed out then end of loop is printed out so what's really useful about this is if imagine we have like a database connection we initialize a database Connection in our main method we're doing all this stuff and then we want to set up a great Soul shutdown type thing we can have it so that whenever we close out this like the Run function or something we can defer closing the database so on the defer whenever that function is done we can close it or if we have like a mutex or something at the top we can lock the mutex and then right underneath we can do defer on unlock face is super clean and easy to deal with it just makes the syntax super nice and clean number eight is going to be new versus make this is a really really critical thing that a lot of new go programmers self-included for a long time don't really understand and the key here that you can base and the key high level takeaway is that make is only ever used with a slice a Channel or a map because what make is doing is make is actually initializing some underlying data structures that are required for these special types required for the slices maps and channels but everything else new or just declaring as a VAR will suffice so what does new do what new will do is we'll initialize and allocate the memory for a new variable but what it'll do is it'll initialize it to the zero values so if I go ahead and right here what I'm doing if I make this test struct up here and I make a new test struct this new function is going to initialize a new test struct with all of the values zeroed out within it so this name will have it zero this string will have a zero value which is just empty string it'll initialize it to have all its zero values new will return a pointer to the zeroed out test struct so right here you can see I 5T is test struct right here it's a pointer to it then over down here I have VAR V and then V is actually just going to return a normal test struct because I'm just declaring a test struct and when you do VAR something or whatever it's just going to initialize that variable to its zero value so different values or structs or whatever and go have different zero values they're typically what you would expect like a string zero value just an empty string an integer is zero a float to zero et cetera et cetera it makes a lot of sense so down here I'm just printing these out and then the slice is different the slice I can't use new to initialize because we have to initialize the length and then there's a bunch of underlying data structures that go has to actually initialize for this because these are special built-in functionalities of the language Maps slices and channels so with the slice what I'm doing is I'm just doing slice colon equals make and then I'm passing it what I want it to be so you would pass in a slice a Channel or a map right here and then some other Arguments for you so here I'm just saying of length 10. so I'm going to be making a slice of length 10 it's going to allocate that it'll zero out all the values if I print this out we'll we will get what you expect so we do go run Main dot go we're gonna get this so we get here we get uh and and then empty brackets so that's going to be T and remember I said it's a pointer so this is actually a reference to uh this test struct it's not the actual test direct itself versus V is the test direct itself so you can see that right here then finally down here with our slice what we're doing is we're just making this slice so we're making a new slice initializing 10 values and they're all going to be equal to zero because they're zero now that's they're zeroed out on allocations we have 10 zeros in here like this that's the difference between make and new I know I've said it many times but I'll say it one last time make is for slices channels and Maps remember that and if you want to learn more go take a look at the Go documentation there's a really great write-up about this highly recommend reading it it is super super helpful number nine is going to be the underscore operator and what this will allow us to do is this will allow us to safely and harmlessly ignore return values from a function or a method or something like that so right here I have this Foo function that's going to return an in and an error typically if I went ahead and I did I if I declared I and I declared error go is going to yell at me because it's a Sim tax error to declare a variable and then not use it so what we can do is we can add an underscore in here and then that will allow us to safely ignore it because the other option would be to just not assign it and bring it out but then Foo is going to yell at me because it needs to initialize at least two variables here so I can do I and then I can do an underscore so this underscore will be ignored we don't care about the error this isn't good practice you should care about errors but for this little example we can just ignore the error with the underscore and then we can go ahead and print out I and it won't yell at us so it's important to remember if you need to safely ignore something use the underscore and for number 10 we have struct methods this is one that I think is really cool it's a really cool thing that go can do and it's if you declare a struct you can declare special methods that will be attached directly to that struct so in this example up here I have type showcase struct so I'm just creating a struct that has a name within it and then what I can do is I can declare a new I can declare a new function in here which is func S Showcase so this initial parameter up here this is actually passing in what I want the struct to be so I'm passing in what struct I want to attach this to up here if we don't pass anything up here it'll just be a normal floating function out in space but if we put something up here it'll attach it to whatever we put in here so I'm attaching this to Showcase then I'm going to say set name and I'm going to pass it name String then set s dot name equals name this is a pointer and I'll go over why in a moment here but then down in here in Maine to actually use this all I have to do is make a new showcase and then now I've attached a method to this struct so I can do showcase.set name showcase and then fmd.purnel and showcase.name I do go run main.go okay showcase so you remember from earlier when we do VAR showcase this is going to initialize it to have an empty string for the name name is going to be an empty string but then I'm setting it to have an actual name here with showcase and the reason I'm able to set it is because I'm passing this in as a pointer so I went ahead and I got rid of this pointer this will now be passing in by value instead of by reference if I run this again we're going to end up getting nothing nothing prints out because this is still just an empty string because I didn't actually set it so we have to make sure we pass it in as a pointer if we want to mutate the underlying struct that's calling it and real quick I want to do a bonus tip this is one thing that I sort of forgot about but that I think is absolutely worth talking about and that is going to be scoping of variables and go how are variables scoped it's entirely based on capitalization so here's an example I'm going to do real quick so within this test I'm going to do Funk test one and then this is just going to be a random function in here which is going to do fmt.println test1 and then we're going to do Funk test two and then we're going to do yep printing out test two so we have these two methods in here and right off the rip this is going to give me a little squiggly line it's going to say test two is unused and why is it saying it's unused but it's not saying that for this one and the reason is because this is public so this is accessible to any other package which Imports the uh which Imports test versus this is not so this is private because it has a lowercase first letter so the way scoping works is it's based on the first letter so I went in here and I did test dot I get test one in here but if I try and do test DOT test two it's going to yell at me because I can't actually get into it I can't actually get this it's going to say test two is not exported by package test and if I want to make this exported I can go ahead and switch this to be test2 I go back over here and then I can say test two so that'll work so it's really important that you remember that scoping is done based on capitalization and this is how you hide stuff from other underlying packages and make sure that you don't just end up importing stuff that you don't want to import it's also how you make sure that you export what you do on export so be super mindful of that remember it's all based on capitalization hopefully you guys enjoyed this 10 quick little tips nothing I know this was pretty basic and pretty beginner focused but I wanted to put out some some of these sort of ground so it's something that sort of goes over the Baseline of go and the real basic stuff within the language I think it's worth covering and I don't see a lot of this enough so I thought it was worth putting out there hope you guys enjoyed have a great day
Info
Channel: Davis Media
Views: 29,040
Rating: undefined out of 5
Keywords: Go, GoLang, Programming, Software, Tutorial, Top 10, List, Beginner Friendly
Id: CK5rLpZk5A8
Channel Id: undefined
Length: 13min 55sec (835 seconds)
Published: Tue Jan 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.