7 Deadly Mistakes Beginner Go Developers Make (and how to fix them)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
some ninjas here to Wallace from my golang dojo today we are going through seven common mistakes that goha beginners often make so that you can submit your next echo review with a more confidence instead of a feeling like an imposter also at the end of this video I will cover a bonus tip that many beginners tend to neglect so make sure to stick around for that before we get started make sure to get your free golang cheat sheet at golangdojo.com cheat sheet that said let's get right into the video the very first mistake a go beginners often make is assuming passing by address is always faster than a passing by value here is a simple example let's say that we have a ninja struct with many different fields for more details about the Ninja at the same time there are two functions one of which it takes in the Ninja as a parameter by value while the other takes Into The Ninja by pointer the assumption that commonly made here is the second function will always perform better because it's a passing it by address which means it doesn't have to do a copy of all of the member Fields however that assumption is not always true if we take these two functions a step further and a return the ninja object and the pointer of the Ninja object respectively and then write a benchmarks for both of these functions we will discover that the first function which it takes in and returns The Ninja by value is actually more performance than the second function which it takes in and returns at The Ninja by address but this doesn't really make much sense right because if we pass an object that by value then we have a tube copy all of the fields in that object compared to passing a by address the key concept understand here to explain this is whether the values are stored in the stack or stored in the Heap often the values are stored in the stack when a copy by a value and values are stored in the Heap when they copy bypoint because the value the pointer is appointing to needs to be kept alive even after the function is a pop-up of the function in the stack this means that the values on the stack are self-cleaning while the values on the Heap require the involvement of the garbage collector additionally if we were to add a concurrency into the mix parameters are passed by value will live in the corresponding go routine function Stacks whereas the parameters passed by address will live in the common Heap and accessible by multiple routines and this again further worsen the performance the next mistake I go beginners often make is not using enough interfaces interfaces in go are very powerful and many features in other languages are Consolidated in go with the usage of interfaces let's take a look at this example from one of my personal projects this is a simplified version of the session management project that I'd worked on as you can see the create and check session function it takes in a parameter of a type sessions input notice that this is a concrete struct type decoder we currently have works perfectly fine if we only have a single implementation of its sessions however as I continued to work on this project I quickly found out I need a different implementation of the sessions if I want to use it as a new implementation I would need to call the create and check session function by providing a new instance of this new implementation now this obviously would work and it will give us a compiling error because the create and check session function it takes in a parameter of a different type now what should we do here should we keep the same function but update the parameter type or should we add an additional function with this new implementation this brute force solutions is simply aren't going to work because they require us to revisit and we Factor our code every time we add a new implementation this is where interfaces are coming to play we can add an interface with a common method and then update the type of the functional parameter to this newly created interface this is especially applicable when you are writing tests for example if you have a stroke with a member variable of a type interface that you want to test because the member variable is of a type of sessions and interface type we can create a market session structure type that implements the interface method with marked behaviors you can use it as Mark struct in order to instantiate the components that you're actually trying to test next let's talk about loot breaks in switch slash select a statement let's use this example that attempts to break the follow but when the channel receives a breaker signal as you can see we have an infinite for Loop even though we have a select statement that attempts to break the for Loop when it receives the breakage signal that's because a break statement and only breaks a for loops and also attempts to break switch and selection statements so in order to have the break statement to break where we intended to we would need to add a label for the for Loop and provide the breakage statements with the label of the for Loop this is a type of mistake takes it can easily be overlooked because it's a runtime error and especially in a casual side project without a full test account the next mistake that the Gold beginners often make is a blindly using third-party Tesla libraries there are actually quite a few Tesla libraries that may be very popular however or not very idiomatic for example here we have two unit tester functions testing the session implementation and that we had as an example audio in this video the first function uses the native testing a package and checks that the result manually with an if statement which is recommended by the go team the second unit test uses the third-party Tesla Library testify which uses a source instead of if a statement at first a glance at these are two functions they do exactly the same thing however there's actually a minor difference the first function where we use is the name native at testing a package actually encourages us to use a fatal instead of error which grants us a more fine-grained control such as terminating the unit tests as sooner and these are potential performance Improvement that can easily add up in a larger and more complex project similarly there are third-party test libraries such as marking of libraries that it typically uses a reflection instead of manually implementing Market struct this I can introduce additional performance to drag as well next we have a variable scoping when working with the go routines now pay special attention for the next 10 seconds because I've got a question for you here we have a simple set of instructions utilizing go routines my question for you is what do you think the three digits are that there's a concurrent program will end up by printing out is it one two three or the same numbers but in a non-deterministic order now for Loops are not I can currently save by default so by when the newly sprung up go routines I tried to print out the integer I the main goal routine has most likely already looped to the last element in the slice s therefore there's a programmer will most likely print out three three and if we actually want to have at the programmer print out all three numbers exactly once and at the same time still take advantage of the go routines that a go provides there are two potential easy solution as you can see both the solutions requires to pay closer attention to the scope of the variables that we pass down to the newly spawned by gold the next mistake go beginners often make is related to the non-addressibility of a certain built-in types in a go let's take a look at it as a simple example of how map values are actually not adjustable the third line here will unfortunately give us a compiling error to fix this we can simply assign the map of value to a standalone variable first this applies it to slices as well as a slice of values are also not addressable now that's a pretty straightforward however what's typically more confusing to go beginners is that concrete evalues are stored in an interface are also not adjustable here we have a ninja struct for demo purposes that has a two method one of which it takes in a parameter as a regular ninja object and the other one takes in a ninja object pointer and we have interfaces and functions that I call those two methods respectively what's confusing to a lot of beginners is how these interface methods and functions are to be called pointer receiver and value receiver method can be called with pointers and values respectively as you would expect and value receiver methods that can be called with a point of values because a pointer can be dereferenced however pointer receiver methods cannot be called with values because a value stored inside of an interface are not addressable another mistake beginners often make is creating these conditional Clause as spaghettis as you can see in this example in the check even and negative function we have quite a few conditioned checks for each integer parameters we pass in we check if this integer is even in or negative resulting in this huge amount of if else a statement additionally if a requirements or changes and we want to add additional parameters the next data statement will look even more ridiculous the fortunate part is this can be easily fixed with early returns as you can see by using only returns we are able to get rid of all of the nesting by getting rid of the invalid cases out of the way first this works especially well in go because we handle Errors By checking if errors are equal to new instead of any other air handling patterns such as the more prevalently seen try catch block one quicker tape that I have to avoid some of the mistakes that I've mentioned in today's video is utilizing linters lenders are code analysis tools a user to flag any bugs or errors in your project in order to make any coding style suggestions golang CI lens for example is one of the most popular lenders for her goho programs installing and running letters like this one is extremely straightforward if you're on a Mac machine for example all you need is three four lines of commands in order to get your very first Atlanta up and running quickly for your gold program I've got another question for you have you personally made any of the mistakes that I've listed in today's video let me know in the comments section down below before you go make sure to get your free golang cheat sheets at golang dojo.com cheat sheet like comment and subscribe and I'll see you ninjas in the very next video
Info
Channel: Golang Dojo
Views: 37,024
Rating: undefined out of 5
Keywords: golang, learn golang, go lang, go language, golang tutorial, go programming, golang for beginners, golang 2022, golang beginner, golang project, golang best practices, golang patterns, golang concurrency, golang goroutine, golang interface, golang linter, golang testing, golang addressibility, golang addressability, golang programming, golang programming for beginners, golang beginner tutorial, golang mistakes, golang newbie mistakes, go developer mistakes, go developers
Id: biGr232TBwc
Channel Id: undefined
Length: 12min 18sec (738 seconds)
Published: Sun Mar 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.