GopherCon UK 2019: Gautam Rege - Impossible Go!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone I love that image don't you I kept it there intentionally so that people are wondering what happens but that's MC Esther and he's a mathematician and an awesome illustration issed he's got a staircase which never ends and of course because it's impossible I said it's a fitting image to the start of our talk well golfers are the theme of the day right so I said why not add some more photos there about it now the idea of this talk is to not test your go skills but ensure that you have at least what the hell just happened moment right you go to see code snippets we're going to run some actual code shoots which is seemingly impossible but that's not it these are all examples that I have actually collated with real-life goof ups and examples so people when there was a situation where there was unexplained piece of code so we'll try and understand why it happened and how it happened and along the way we'll have some fun now if I don't want to talk to be too heavy so I'm also going to talk about I'm sure you're loosely looking at that last slide what I'm going to talk about a few confessions as an entrepreneur and a little bit about me my company's name is Josh not Josh there is no Josh in my company i co-founded this company twelve years ago I've been programming for about 20 years and I do believe that programming is an art my company work exclusively in Ruby and go and then of course we have to support all react and native Android iOS I had the privilege of authoring a couple of books and I should also put out a few disclaimers my first confession this is unknown she's also been the co-author of this talk and the confession is that you know I'm almost twice as old as years nobody heard that but I have no qualms in and asking to review my cord alright and these are all the things that we've learned the bottom line the takeaway from this talk is read the specifications the goal specifications document specifications are amazing the problem is they're super concise every word one additional word and suddenly it's a gotcha moment in realizing oh is that how it is so are we ready are we ready are we ready okay so I'll start I'll build up the tempo slowly and we'll start looking at some easy simple examples the problem with such giving such a talk is there I have a complete varied audience now there might be somebody who is very new to go there are people who've been there and probably thinking what is this joker talking about the stage it's obvious but if everyone of you all has at least one moment where it says haha my aim is achieved are you ready simple first all right I have an array of eight elements I'm taking a slice out of it and I won't ask y'all what this is this is this is the slice and this is the array all right yes I'm right so far so good I'm going to do some simple things just to warm up get your get your minds churning I've manipulated the slice what's the expected output what's the manipulation the slice in the array do both change yeah both change why because the slice is is peeking into the array let's what happens now I'm appending to the slice what happens is it a new slice getting created is the same array getting changed both change well done so the array gets manipulated slices changed let's push this a little more what happens now so for I'm hoping people who understood the syntax there is just the flat array I'm inserting three elements on the trot why should it be different from the previous one so yeah so here's the case er obvious hey the slice changed the a didn't now if a few of you all have had one of these moments smile okay let's let's dig it little deeper into this and I'll go fast on this part because y'all know go this is how how slice is it's a struct which has the length the capacity and it's a pointer into the array note that the capacity of the slices 5y5 because that's the length of the array and when I manipulate something it's automatically manipulating the same memory space when I append something it's a pair is manipulating the array however when I added three more elements it's crossed the capacity so the slice reallocate the complete copy of this now what is the problem you see in this your code is working fine for some time and suddenly you realize that hey something's gone wrong the take home from this part is that you want to be very sure when you're working with an array and then slicing off it because you're going to be you're gonna end up in a state where your array is completely manipulated sometimes and your slice keeps working so your slice is working fine and yeah isn't so blare of these slice in Airy Wars so you'll feel a warmed up let me detox y'all alright that this is you know all the stars that you see out there the confessions of an entrepreneur I started in 2007 me and my co-founder only two of us right and I knew only one thing life how to code now there were so many people who came in dissuaded us from starting up citing various reasons that you know our larger company requires finance skills and management and you have to give up coding and you have to look into the sales and marketing and with money and we were worried we were scared to put out a few other myths I had I've just a basic college education qualification but a passionate programmer I did have any connection of contacts in higher places who could bail me out and give me some work and moreover I didn't have any money because I started after 8 years of programming and well he's a pretty well-paying job why didn't I have any money because I have funded a lot of pubs in my local city back home in Pune so you used to only go party three times a week but that's not counting the weekends so yeah well and I can I can put that to a test it's helped me out right we can meet in the post conference party and we can test that theory out but what we did have was passion my company's name is Josh and Josh in my mother tongue Hindi means the enthusiasm and passion and that's why we started this company main because as a programmer what is your toughest job naming functions so we said hey man this all we can do let's just do only this okay today after 12 years we were 106 programmers working in Ruby or go of you or 85 clients the passion is still strong I didn't want to mix services with a product I'm sure there are plenty of entrepreneurs been-there-done-that entrepreneurs successful entrepreneurs failed and the pillars and the people in the audience who feel I will start up one day if you're one of them my sincere advice is do not try to mix the two don't try to fund your product by the services you are rendering you'd probably mess it up so what we've done is we've actually started putting our separate companies every time we have a product idea one is in inshore tech space one is an IOT smart metering company one is in the retail space one is in the online car auctioning space and we invest in these companies so it's a good medium to actually diversify enjoy the way you think building things and yes I have already shot down two companies of these products which didn't work but nothing I repeat nothing works without passion and if you are passionate about something and you're following that up success is about guaranteed I'm keeping my fingers crossed because every year I lose more hair so I I don't know what happens when I lose all my hair well back to business are you ready alright all of your the answers to these next few slides are either true or false what's a and if you have an opinion it's always awesome to shout out loudly true false false false true well it's a compiling error and this is this is all of y'all have been through this it's it's a compiling error because even though the value of the integer through the type or through the native int is twelve go is going to look at them as different types so it will not even allow you to compile because in as for all go programmers there's only one way to do things right and that's the only way it will allow you unlike other languages here's a funny story you know I've been I used to be able aw I have been giving a lot of talks at Ruby conferences where I make exactly the opposite statement well in Ruby you can do the same thing in ten different ways because it's a human language you know it's fun to do things in different ways and when I'm talking go it's like hey there's only one right way to do things so uh yes I'm a Salesman well look at the slide and let's try to push the limits a little more I'm doing the same thing with the start I have a type foo struct because I like foo and I had an anonymous struct and I'm going to compare the two the data in the struct is absolutely right I am 18 years old right and the data is the same the values are the same the type 1 is a struct type named type and one is anonymous struct what's the output true false true if you think it's true shout out loudly true if it's false but that doesn't mean that side shouts right it's just compiling errors anyone all right Amma chiefed hey somebody got it right yes this is true because only for struct go is going to look at the internal native types too and check them carefully let's push it further I'm doing the same thing Oh t-shirt on this one so I have these t-shirts a few of them available all right let's see what gives the answer a strict strong confident proud answer gets a t-shirt from me all I've done is just put them out into another method to compare the same data elephant I think we should talk elephants the right answer yes anyone false and the teacher chores if you're able to ants tell me why it's false yes perfect so here's the kicker the interface data type checks the dynamic type of the underlying struct so at this time it looks at foo and an enormous and it's a struct so it's false subtle things which are blood boiling doesn't alright the other way let's try the reverse way on this now the answer to this riddle is true but my data is different but my data is different what's the magic code here and sir please come and meet me after the talk and collect your teacher from me and what's the magic code here any takers you'll have all seen the underscore being used we use it very often right our damn that's an error I don't want handle it underscore anymore the blank field you put a blank field you typically a blank field in a struct is going to be used when you want to bag the structure for mostly transmissions for any kind of protocol buffer stuff like that but it is not compared now here's the cool part this is there in the specifications and it's it's very concisely put a struct will compare all the fields except non blank fringe how often do you end up reading you know it's a common myth that when you read a sentence you tend to skip three out of the words there and yeah you know the meaning because you're just looking up the book picking up the context there the goal documentation ladies and gentlemen please read it extremely carefully because every word has a meaning in it this bit us a lot somewhere and we were wondering why this thing is not working okay let's continue the game true/false hey this is this is this is the goobie and I had promised I promised mark backstage that I am NOT going to talk about how England won the Cricket World Cup but not the final right and the Google age is brought back all the memories but yeah this is straightforward right everyone knows this what about this false true true anyone well say this is a compelling error it is a compiling error so slices maps functions cannot use the Equality operator because they are not direct they're all there in directed because they are structs internally and they have pointers in their quick check with you for the rules here all these rules are actually examples that have been done in the past here for the type the types must match except venture struct very barring the struct fees all the values must be identical except the blank fields maps slices and functions go will not dereference it so it's one level check this is the reason why type checks and go are super fast and accurate unlike languages in the past like Java it will go with type inferencing all up the hierarchy to see if this genuinely the type and go down drill down to the native datatype and check it which slows things down the only exception is the interface datatype where if it will do the first level check on the dynamic type ok wand up i all warmed up okay you know what when you talk about comparisons i have to detox again and I have to confess about my life's failures you know when when you have started up and I'm sure there how many entrepreneurs in the career who started up right I'm sure you've always had a problem of no money alright this has been a time my time was September 2010 we had eight dollars in our account we had to pay salaries at the powered seven thousand eight thousand dollars I goat I have no option like I told you I had no savings I had nothing so we go to the bank and say we will take an unsecured loan and the banks are like yeah sure man come give you an unsecured loan like yay somebody is supporting us what a fantastic economy the banks are our friend and he comes back with the document a week later and shows me a like hey dude the rate of interest you when we spoke you said it is like about 12% now you just showed it is like 17% I mean that's not fair like he looks at me in the eye this is bankers for you he looks at me in the eye like yes sir I agree but it is going to take us two weeks to change the documentation and get back to you but will your company survive till then so I took the paper signed it I gave it back to him and said don't ever come in front of me again but that's people go to let you down your finances your banks are definitely going to let you down that money actually got us out of the hole and life was good after that we were able to actually sustain get some more customers so this every time you hit that one deciding factor where you want to sit down hold your head pull out the rest of your hair right just hang on just hang on there is always there well your customers are going to let you down we had a case where our customer is you know you're a startup you're always going to say yes sir yes ma'am I definitely want to okay my payments are delayed by a month and one more month and after three months I got an email hey you know what we've declared bankruptcy you can keep the source code I don't like what do I do with the source code it's not toilet paper for me he's like yeah but we've gone bankrupt but the weird part was bankruptcy in the u.s. is weird you could be super rich but your company could go bankrupt since guys living in a mansion in the hill with fancy cars and a swimming pool but it's refusing to help us survive so your customers are going to let you down be prepared for it hell you can you know your banks will let you down your customer will let down people even your team let you down now who your has deleted a production database but before deleting the production database as the good developer you deleted all the backups first because there's too much of these too much of back up side first delete all the backups then I'll back up the production and by mistake oops there goes the production database yeah we did it we did it and then we were we were tested for our limits of trying to how long can you stay awake yourself Oh Mike my capacity or seven days so you're in the office for seven days and nights trying to restructure that data from the logs people we we take logging so lightly sometimes those logs will come and save your ass one day log everything now a question might pop up what happened to that developer who actually deleted the production database we gave him a raise and fixed the process yeah guys still with us after like 12 years he's still with us and one of them one of the most efficient reliable developers in my company everyone makes a mistake once in a life the question is as an entrepreneur can do you have the ability to respond if you just click the words responsibility into the ability to respond you may not be right but did you take a decision is all that matters this this simple mantra has helped me out a lot over the years where it does not matter whether you've taken the right decision lost a client I had a customer whom I was trying to tell them why you should not work within our DBMS and you should work with the no sequel database because this is how this is the right approach for your particular product and he tells me dude but my friend tells me that we should use my sequel oh sorry sequel server Microsoft sequel server and like do you have open source tools for this please don't use an AR DBMS it is easier no no my friend and I say dude it's the my way or the highway and my customer said okay highway oopsie but the question was taking a decision and sticking by it a lot of the times if you're going to back down we are probably going to end up with the situation where you're going to tell your customer I told you so but that has no value later on so respond and have the ability to respond and you're probably in good shape okay back to business again only this time I'm going to actually run the code any takers for the output so I'll just explain this this is just a it's a map and I'm just printing the map that's all I'm doing what's going to happen I'm a whim user okay I have always used win and I've tried to use everything else and I always just come back to him is this visible okay all right I've always come back to him so I'm going to run this code and it brings prints the output so far so good all I'll do is I'll run this program again have you noticed that the output is different right why because no I'm sorry I didn't hear that clearly but did you mean that it intentionally randomizes the order so here's where I come up the concept of programming ethics the language will intentionally randomize the order so that as a programmer you don't rely on the insertion order when you're used when you're iterating and iterating a map most languages have seen this I've seen this in Ruby where the where the map that the hash is actually this order is maintained so the unfortunate you know bad coding practices come in like when in doubt just use a map because you can treat it like an array you iterate the map it's in the you iterate the map you will get it in the same order and then you look it up you get whatever you want so when in doubt just use a hash that's wrong gold tells you that if you're going to use a map you're supposed to use it for a lookup so in case you need to iterate it ensure that as a programmer you are not depending on the insertion order and this brings up a lot of other concepts in go where it is trying to tell you there is one way of doing things do it right okay some more code there would this code actually work we know I know all of you all know that method overloading a function overloading is not supported in go so for I'm pretty sure there are few folks of you who already know the answer here you will get your t-shirt but hold the thoughts this is the this is the piece of corn then I dippin that we just saw I'm creating a person with different number of parameters should it work yes it should work how many field think it just is the compiling error here so it's the same method being called with different parameters that code actually works and this core is working with all the default values that I put in for those Global's now is the time hold hold it you know the answer people who know the answer raise your hand awesome okay if the rest of y'all are thinking what just happened it's a win but this is a very interesting concept all right there's somebody here who actually user you had what I do okay but the data types for name age height and weight are of different data type so they're not strings no interfaces so there are no interfaces you're no strings yes no strings yeah it is a very it is a very addict function of course because I have multiple number of parameters but what am i doing no no reflect no reflect no type of session I'm not using I'm not passing strings by the name and trying to evaluate it anyone I'm sorry I know it's it's goal so it's not a slice here so even if I take it in a slice it still me so that that's what it means by very DIAC function but the question is there of different data types too so it's a slice of what interface data type no there are easier ways to do this yes sir very addict it cannot be very addict for each type otherwise things like this would fail this is a string this is an integer yes this is new person is a method is the same method yes sir agreed but I've done something even different there is no it is a very attic function agreed but there are no interfaces no interface data type used in this code if it wasn't this is the same thing if you use an anonymous struct it still doesn't help because I have passed different variables no new new person is a method that's a good question new person is not it's a it's a method yes this this is the only start I have in the entire course there are no there are no constants in this Court you you parse it and iterate it and see if the string is the same name no my order is different to you man all these are functions you got yourself a t-shirt here's the code here's the code where I have a very action it's perfect but I'm taking I'm taking a function as a very direct function and I am returning this but I had told you all earlier that see this is this is an easy way to just have some fun right I have told you all that all these examples are from practical practical that we have been through so where would this actually work functional options function options or functional parameters are actually a very neat cool way to have different types of configurations everywhere for example if I want to initialize a server with TLS or the timeout I can just specify them if you all have looked at the config struct in most packages you would see that that config strap is huge and you have to set every field there if it's a nil field it's parsing in world these are easier ways to do it so if you actually setup the defaults for a new server you can choose which option to override just by passing a function so you can even pass it parameters this is an easier better way to do this Dave Cheney actually gave an entire talk in dot go of years ago on functional options and I actually had gone down there and asked him like dude why didn't go just support default parameters equal to and stuff in most languages and he take us to answer that question why are now why don't it most languages now started supporting default parameters with equal to right when you are declaring the method it's a nightmare for parsing that code and guaranteeing that it's always done right the complexities of your parser in your compiler is insane so the decision was taken not to support it be sweet be simple and always have only one way to get it right we saw this piece of code because in the company that we are trying to set up we had to integrate with 29 different insurance companies with all kinds of different protocols and we interfaced a method called you know Connect or open but there were so many different types of configurations that the strux had become insane so instead of trying to just bloat the court we actually started using functional options and it worked interestingly if you look at the the dates on drop bikes and and Dave Cheney's post this is that older I strongly recommend you all to have a look at these things because in fact rock bikes post is awesome it takes this to an entirely new level but I've just manipulated it but your question could probably also be hey man you know what I don't want these constants so effectively I'm setting the defaults here imagine this function returning a function which gets executed later on by the methods that I'll have said earlier by the parameter so that that blog post actually deals a lot into this have I thoroughly confused at least a few of y'all that I take as a yes all right continue okay I'll explain this stuff here first I have a string adjacent string which I'm trying to unmarshal into this particular struct called superhero that strut does not have anything other than an interface in it but this interface is implemented by these kind of starts should I repeat what repeat just what I explained is the question before even I asked for the answer should it work in other words wouldn't it be cool if just by manipulating the JSON data I could get different kind types of superheroes instantiated all right let's have some fun first then we'll come back here's the code here's the code I have a superhero struct which has the powers interface and just a caveat usually when you define an interface you always have ending with ER or but in this case it just didn't make sense power is already there so I just put it as powers I have Ironman and I have tano's and I'm just going to unmarshal the data I'm going to unmarshal the data here should this work for example this is the implementation of Iron Man and Thanos here okay so I've implemented the method should print Iron Man if it's Iron Man in tennis if it's tennis all I'm going to do now is is Iron Man immortal now he died right in him [Applause] Spyder any fly yeah and he's super fast all right and I don't care about super-strength because it's not there and destruct well let's see what happens I didn't haven't changed any code let's see if his tennis immortal sort of let's let's make him immortal yeah he was wasn't he can he fly so I'll just make that false and I think he has super strength so here what I've done is I've just changed the the data in a string and I'm running the code again what did I do right if I'm sure you know the answer hold on and you already got yourself a t-shirt oh that's cool ah cool don't worry I don't have any t-shirts I'm just joking alright so what did I do we have one person in the audience I'm sure he knows the answer yes the problem is that if I try to run this code as is I will get this error which is absolutely right I cannot unmarshal some random string into some interface because it's just too complicated isn't this too freakin complicated go agrees the compiler degrees do don't do these things so this is the error you will get but damn the code did redundant it the key so there is this method called unmarshal Jason which you can call on any receiver in this particular case the superhero pointer and you can put in any kind of logic and here you will see the struct that I've taken as a as a map and I'm actually instantiating those particulars struts to the right interfaces simple and smooth but the key was here is unmarshal Jason which is right below in that code which I don't show the hell because you see I have a WTF hero there too because there are chances that I might not might not have one of these fuse right so coming to the last part well this is something we actually found out when we were looking one of our projects for security project how many of you have heard of Dex or work - Dex alright so next is like an open ID you know open ID connect federated provider where we had to go through a lot of different kinds of connectors and this uses this technique of using a configuration which returns the function to try and open an interface and then we had to drill that down and the last impossible code there is this is the main function there is no wait groups there are no channels what happens and he shut up for grabs the non-existent one same code I'm going to run it this code hangs sorry so the I will change this a little bit I will change this to seven go routines not eight I've changed it to seven go routines this should give you the clue this should give you the clue of why these exits here's the the other coupon I'm keeping it back to eight and I'm just putting a printf there so it will run for some time but exit after some time this was this bit us in a Bart a lot when we were so this is the reason why because go routines are unlike most pre-emptive garbage collectors and pre-emptive threats go routines run if you're giving it a compute it will run and it will occupy the core it will occupy the processor there and I have because there are eight processors on this machine eight go routines can occupy a processor and completely make it unusable these are the only cases where the go routine will actually switch context we've done this in in one of our projects where we had a Raspberry Pi and an R Dino coming together trying to figure things out right that by the way that so that's a winel player without a pivot it's running on microcontrollers and I'd love to talk about that maybe give a demonstration sometime part I've already I already see mark there and he's promised me that he's going to come on stage and kick me out man come on mark you know why you know why because as part of entrepreneurship you have to remember that you know it's for life guys enjoy yourself sometimes even take a break all right and I don't know how many of you all can do it but try to retire by 40 you retire by 40 guys and it's just easy because at that age then you can probably do what you want you code because you want to not because you have to you I love to travel I love to talk and I get to get mark and me fight on stage so have fun around that well before I go and last slide after this I am this talk is about impossible stuff so this particular case I'm actually going to the impossible and not say thank you
Info
Channel: GopherCon UK
Views: 4,070
Rating: undefined out of 5
Keywords: computer science, technology
Id: jZ1ZsULRyE0
Channel Id: undefined
Length: 44min 5sec (2645 seconds)
Published: Wed Oct 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.