 Welcome everybody. We are going to hear to Max Tepkev. He will talk about Go if we need to go and switch to this language from Python. We'll have a few questions at the end So welcome to Max Tepkev. Hi everyone. Can you hear me well? Okay. Thanks so probably wasn't easy to get up for a lot of people and For me as well because of the pinch of fire and other stuff which is going on. So thanks for coming. I really appreciate that Let's find out if you're still on the right talk I mean if you are already programming in going for like two or three months, then probably you won't find Something new in this talk for your for yourself. I'm not saying that you should go away or something like that you can can stay but just keep it informed and And the second second if you visited the talk about Golang Yesterday, you'll probably notice that I will repeat some things because it's hard not to repeat facts So just I don't know you can post something to Facebook while I'm repeating these things or Checking your email But we'll also discuss a lot of things that you didn't hear yesterday So the goal of the talk is to answer the question Do I need to switch to Golang? Not me, of course, but you because I already made a decision And you probably wondering why am I giving this talk on the Python conference? Well, because for some reason it happened so that a lot of people who program in Python are Also using go for some reason. It's really really easy to Use go if you know Python We'll try to figure out why during the talk But first just a little brief introduction about me and my company My name is Max. I live in Moscow, Russia. I'm a Python and Golang developer I'm the author of several Python libraries, which you can see on this slide and I'm a European speakers since 2014 in Berlin and I also give talks on other conferences from time to time And contribute to our other open source libraries. I work for the company called a data We collect and process online and offline data To get the idea user data to get the idea of users interests intentions demography and so on We process more than 1 billion unique users to month and there are more than 2,000 segments in our database like Users who are interested in buying a specific car brand or users who like to travel to Spain and so on We have partners like Google, Oracle, Microsoft, App and Access and many more We have quite a worldwide user coverage And one of the biggest coverage in Russia and East in Europe for example for Russia It's about 80% of all users and That's it. I promise it would be very brief So let's get back to the topic. The question was do I need to switch to Golang? But let's figure out why switch from Python anyway So the main reason is speed Python is just not fast enough for a lot of tasks. There are things like Different ways to make Python faster like Pipeye Scython and friends and so on and just the existence of these tools proves that this is a problem. It exists Then comes concurrency While Python provides us with different ways to solve concurrent tasks to make concurrent programming There was no defect of standard until Python 3.4 when I think I was introduced but in standard library still it will take a lot of time for people to To adapt to it to get used to it to know how to do things properly with it and to switch to Python 3 of course Gil it's the most discussed Python problem While you may use multi-processing to avoid deal still in the process communication is slow So this leads us to problem number one Python doesn't provide a way to to make two binaries by I mean binaries with machine code Then you can just drop into the server and run. There are things like CX freeze and so on but this is just Just an executable archive with Python stuff. So and also you can't write closed source code with Python. So you can't easily distribute your commercial programs Because Python code can be easily restored And while dynamic typing provides us with ease of use it is also a big source of runtime errors and The proof of this is the introduction of type hinting in Python 3.5 So it's also like a problem Okay, so let's briefly formulate language requirements For the language of choice, which will solve our problems with Python It should be modern by more than I mean that at least it should have proper unicode support and Use all the powers of modern CPUs, right like multi-core support and so on It should be blazingly fast ideally fast Like CC plus plus close to its speed It should be easy to learn and again ideally you shouldn't require manual working with memory It should compile to two binaries with machine code, of course it should be statically typed and Which together with the previous requirement will mean that will identify a lot of errors on compilation time and not a runtime And it should support the main platforms, of course like Windows, Linux and Mac So why go? I mean there are other languages, right, which can fulfill our requirements And I believe this slide kind of answer the question Because we forgot two more requirements for our language of choice in our previous slide The community in how widespread the language is so the first number here is the amount of questions asked in total on stack or flow by tech and the As of yesterday I took these numbers yesterday and the second number is the amount of new questions asked For the last month. So as you can see goes clear winner here Also, I want to show you this Google Trends graph which shows searching activity for term go lang We can clearly see here that most more and more searches search requests are done every year and popularity is constantly increasing To a little bit of history about go Go started as an experiment in Google at 2007 to address some of the problems Google had like slow builds working distributed environments and so on There was even a myth that go was conceived during one of those slow builds while programmers were waiting for program to compile I won't go into details about why go was designed the way it is now because We would need another 45 minutes or even more for this if you could if you're interested you can read the link at the end of the slide I'll just tell you the go was open sourced in 2009 and it is created by It was created by a lot of smart people people who created things like Unix UTF-8 Java hotspot virtual machine and a lot of others things that we use every day as programmers Go is already used in a lot of companies throughout the world for example Google Dropbox and so on There's a giant list of companies Again at the link of the end of the slide So you can visit and have a look if you're interested Now let's have a look at how a typical go project look like This is called the workspace the workspace is a directory where all your go code lives It is defined by setting a go path Environmental variable it consists of three directors spin packaging source source is where you put all your source code and your code dependencies and being is for binaries and package for the package objects There is no single repository for packages and go like there is pi pi in python So to install a package you simply type go get and URL to a package repository From one side. It's simple and cool from the other side. There is no way to specify the version of your dependencies There are no things like requirements dot txt and something like that which can freeze your versions of the requirements So basically when you do go get package foo it downloads source files Of package foo scans them for input statements to identify dependencies then downloads files of The current master branch of which dependency and then compiles all source files all together into binary This is a big problem because if a backward incompatible change was introduced to Master branch of at least one dependency and you do go get that you end up with the broken program Go developers realized that and starting from go 1.5 Enable the so-called vendor experiment and starting from 1.6, which is the current version of go It's not an experiment anymore But they recommend a strategy It means that you should include a vendor folder in your project and put all your dependencies in there By copying the source code the source code and they will be found found by go compiler automatically I know how it probably sounds to Python programmer like vendering what the you know but actually it's okay because a lot of a lot of Programs in Python also vendors things for example peep requests which we all using like and I Also started to vendor things you can throw it matter with me if you want but actually then there isn't that bad because If it's done properly, it can really help your users in the end You just have to do it right There are also two more popular ways to solve a package management problem in go Go packaging and go get the bed or IO, but I won't talk about them You can Google if you're interested and there are also a few Other approaches to package management in go. We won't discuss them of course But I like the name of the last one Johnny Depps So go comes with a lot of helpful comments built in Go format which formats your code in the only right way if you don't like how it formats your code and sorry you're wrong It's like pepe it in Python, but most week Go test runs your tests and shows you desk coverage go fix Updates your code to use newer APIs if they were introduced if some somehow changed in a new version of go Go run compiles your code into binary then immediately runs it and Delets it in the end So it's a kind of a scripting language simulation and it works really nice because go compile time is very small So This is why a lot of system administrators started to use go with a scripting language There is also a race detector and go that comment Which can find errors that compiler didn't see it compile time and many more and all these things They are built in right into the language. So you don't need to install any third-party tools and so on So let's have a look at the hello world application Each file should start with a package definition, which will be used for imports by other packages An executable comment must always use package main Then we import format package from go standard library and define a main function Which just prints hello world? To compile this code we run the following comment Which produces a binary now workspace bin directory and then we can just run it like usual binary To let's do a comparison of go in Python what I want I want to show you that some things are different, but a lot of things are similar So it's this is one of the reasons why it's easy for a Python programmer to learn go to first of all imports Both languages use imports to input packages. The only difference is that In going must surround package name with double quotes Also import statement differs a bit if you want to import several packages or to import a package under different name When you import a package you can use names which define inside Inside the package in the same way in both languages The main difference is that in go you can only access names which starts with the capital letter and All other names are considered unexplored or private if you like that term So they can't be accessed outside the package Go has same basic types as Python with a few differences because go is statical type language so If we have initializers, then we don't need to define types because go is smart enough to figure out it from them If we don't have them that we need to define type Also go has signed and unsigned in types which with different bit sizes like in dating 16 and so on and also a dicting Python is called map and go and note note that map Defines types of its keys and values. So in this example, we have a map, which is in values are signed integers And finally a list is called the slicing go Variable declarations have more forms in golden Python In Python, we just declare variables any way we want with the same syntax in go. There are two syntaxes full variable declaration with our keyword and Short variable short form of variable declaration, which can be used in functions or methods, but not at the package level we can also have one line variable declarations in both languages and In go in both forms full and short Both languages have first-class functions. It means that the language supports passing functions as arguments To two other functions returning them as values from other functions And the signing them to variables Though as you can see the syntax is different goes more verbose because of static types and see like syntax with curly bases Both languages also support optional positional functional arguments in Python we call the marks and use asterisks for them asterisk them and in go functions that implement them are called the erratic functions and use three dots syntax notation for them and As you can see we can also pass a slice of numbers in go and using back-end feature similar to what we have in Python Unfortunately go doesn't have a keyword arguments feature, but you can kind of emulate it with structs Python doesn't have structs, but basically just the collection of fields Struct can be initialized only with the needed fields or fields that are not said you in initialization will be set to the zero value So for example for string type. It's an empty string This is probably not very elegant and not the idiomatic way to do things in go But it works if you need it. So just showing you that it's possible to emulate quarks in go Python has four in wire loops go has only four loop which can take several forms Here we initialize a list of three integers and then iterate its elements one by one Notice that the first variable in for loop in go with which this is the underscore And this is an index of the element inside the list and because we don't need it We use underscore and this is the same convention in Python, but in Python. This is just a convention I mean you can use any variable name for it and just don't not to use it But in go you have to use an underscore because if you don't use Your very you declare a variable and you don't use it or you have an important you don't use it then the program Just won't compile Go is very very strict about this and that leads to a cleaner code in the end second form of the for loop can be compared with the Python's while loop with the condition The code is simple so I won't comment on it Just know that you can omit in it and post statements in for loop also go has variable plus plus syntax available, which Python doesn't have and Both languages have land function to get the lengths of the variable And the third form is the while loop with infinite condition We all love Python for its list and dick comprehensives. I'm sorry, but there are nothing seemingly in go Whenever you want to iterate over something you'll just have only one looping construct Which is available in go a for loop Some people already asked to it's something similar to go But go developers took the they should be one and only one way of doing things really seriously maybe too seriously and It looks like that we won't see any think tactic sugar added to the language in the near time To for loop is our only friend for ages probably You can also notice that comment syntax is a little bit different in go see like Let's talk about conditionals a little bit Both languages have if statements which look pretty much the same with money one exception go allows you to declare variables inside the if statement and These variables are only available during the execution of if statement. I mean they won't pollute your name space This is usually in our current example, but it can be very handy a lot of situations and Really nice feature thing Go also has switch statement Which can be compared to Python if Ali fails chain Notice that which statement also allows us to declare a variable Inside itself and it also won't be available after switch finishes its execution You can of course write the same if else if else chain in go, but in most cases which statement just Looks much much cleaner and readable Go has the same slice and syntax available as in Python and that is awesome. It's all you need to know about it Now comes error handling, this is probably one of the most discussed topics about go So in Python we use exceptions to handle errors and in go there are no exceptions the theomatic way in go to check for for error values is There I mean if a function can return should return a variable and an error value So if this error value is new then everything is fine. Everything is working fine If it's not new then then something is wrong and we should act appropriately So why is this? One of the most discussed topics about go a lot of people think that a language from the 21st century should have exceptions and This kind of error handling is like 70th style or something like that So a lot of people aren't happy about that and when you start programming in go you will notice that a lot of a Lot of a big part of your code is that these if statements that check for errors Very verbose Anyway, go developers. They just don't agree with this. They think that it's Very cool. It's the proper way of doing things so We just have to leave with that I think Actually, there are also panic and recover functions which kind of works like exceptions, but not really in a technical sense So I won't go into details about them. Just know that they are here. So if you Are interested just Google for them Okay Classes I have bad news for you go doesn't have classes. It has structs and you can attach methods to them To make a function act as a method you need to define a special receiver argument This is very similar to Python where a receiver argument is called self the the receiver appears in its own argument list between the Funk keyword and the method name in this example P is the receiver argument of type person Go has a convention that a receiver argument should have a one character name that is the first letter with style Also note How we are magically using string method and go to change the string representation of a person type So when you when you when you print it, this is kind of similar to Python's Python magic str method But in go this is achieved using interfaces Which we won't talk about today because this is big topic and Actually, you can attach Methods not only structs, but to any other types to integers to strings to functions to whatever Structs in go doesn't have constructors But there is a convention to emulate the constructor by creating a function that does the need initialization if you need any This is our previous person structs initialization written using the constructor function We can also see a new syntax here notice the asterisk near the person type at the function definition That means that the the function will return a pointer to a person type And also notice the ampersand inside function. That is a way to return a pointer to a type Yes, go does have pointers, but again, we won't discuss them today Okay, if you words about inheritance go doesn't have one again Instead it has composition or embedding you can embed parents truck inside a child struct And reuse its its methods like if they were defined on the child struct Let's have a look at the example on the left. We have a classic Python inheritance and On the right. We have goes embedding Doctor type embeds person type and gains access to its it method as if it was defined on doctor type Kind of Okay, let's switch to some cool stuff We all know that Python has something called context managers Here's a well-known example, we chose the usage of context manager to read a file and automatically close it in the end Go doesn't have context managers, but does have something similar Which is called a deferred a defer keyword can be used together with any function inside another function and When a function inside which it the first statement is used finishes its execution It the first statement will be called just before exiting a function. It sounds weird, but I Hope the example can clarify that a bit so We are reading a file in that function go and we have a defer F close and it doesn't matter if any error will happen or something else The file will be closed anyway. We can also see difference in style here in Python We name a function or a method using underscore between words and In go a convention is to use camel case also notice the How we use the result variable inside a function definition and go This is called the name return value and is treated as Variable defined at the top of the function with a zero value and we already know that the zero value for string is empty string Okay, last comparison concurrent programming I hope you're able to see the code Because I hadn't had to make a font a little bit smaller To fit everything in one slide. So let's start with the amount of code. Usually Python is the winner here We have to type much much less code in Python, but this is not the case Here we have two similar programs which can currently retrieve the content of two sites Nothing really to discuss about the Python version I'm using the async.io together with AO HTTP package which should be installed via pip and async wait syntax, which is available starting from Python 3.5 Let's talk about go version of the code in go Concurrent program and is built in writing to the language, which is really really awesome First we define a list of URLs to fetch then we create a channel for HTTP responses Channels are used to communicate between different parts of the program Then for each URL and list we start the go routine, which is just a usual function that runs asynchronously and Fetches response and sends it into the response channel To create a go routine, we just depend the go keyword and that's it After that we start to listen to messages in response channel and as soon as new message is received We immediately print it to standard output Also note we're using a defer statement here to close response body and We have to cast body to a string because we don't return body as bytes This is just the details Also, I'm not doing any error handling handling neither in Python nor in go just intentionally here because To make to make code shorter and to show you only what's important So from my point of view concurrent programming in go is much cleaner and understandable than in Python even with async.io So there are more let's name some some go features if we didn't discuss but They are here. So Making new functions that they use to initialize different types in go erase which are the basis for slices Pointers that hold the memory address of the variable Interfaces provide a way to specify a behavior of an object if something can do this then it can be used here kind of a dark typing in Python type assertions and type switches which are used with interfaces to cast them to concrete type Advanced techniques for concurrent programming like buffer channels and select statements Unsafe package which allows us to bypass type safety of go programs if needed for example to interact with C or C++ and starting from From go 1.5. We can easily cross compile our programs with four different platforms just by changing a few environmental variables Features that collects list and big comprehensions There are no generators in go, but we can use go routines and channels to achieve the same behavior There are no decorators in go, but we can easily wrap a function with a function to achieve the same behavior No exceptions Go also doesn't have meter classes descriptors and Magic methods and probably never will but who knows there are also no set and tuples So conclusion both languages have good standard library and a lot of third party tools for almost everything That's not actually really true because go doesn't have any good stuff for scientific programming like pandas, numpy and so on but go is relatively young so I think that Something something will be created eventually YouTube pythons Syntax and syntactic sugar We can be much much more productive in Python because we don't need to re-implement things again and again So for example if we need to to get dictionary skis We just call kiss method if you want to check if something is contained in a list We just write if something is in list and so on but in go every time You have to start this for loop to iterate or something and to get what you want so this is really verbose but Again, this is what we have On the other hand goes Concurrency features and it's compiled and statically typed nature are worth it. I think so You can easily develop commercial software with go and sell the binaries keeping your source code secure Also, yes, you will spend more time coding in go But in the end I believe that programs which are written in go are more secure because a lot of errors are found at compile time and Also, if you're developing something that should benefit from concurrent execution then go is just a very good choice for this kind of things And the last point that I would like to address Maybe it sounds stupid, but I think that go doesn't have soul I mean, I'll try to explain I mean when I'm coding programming something in Python I feel myself like an artist which creates some masterpiece or something a hopefully masterpiece and In go You just I don't know you don't feel any happiness any satisfaction You know during development you just I Don't know you just have to try it. Maybe just me but this is what I feel and Let's try to answer main question. Do you need to switch to go? Of course not Python is great and But do you need to learn go? Yes, definitely. Yes learn go because these languages won't go away anywhere Because it's made by Google Also The funny thing is that a lot of companies that use Python also use go Or vice versa So if you are if you are interested In getting a good job or getting a promotion or something like that then go is the the right language to learn We don't have time anymore, right I Just want to show a few useful links So you can make a photo if you want they will help you in learning go and Yes, I think that's it and if you're interested I am not paid by Google to promote their language So I've just shared my experience with it And I hope I was able to motivate you somehow to try go and if at least somebody of you will Try go then it was worth it. Thanks Thank you max. So now we have a bit of time for questions wait for the microphone, please Hi, thanks. This talk was very useful to me. I'm a go newbie and the most difficult thing I found when maintaining an Older go program meaning like it was a year old was that the standard library seems to be changing a lot I was wondering if you had any experience dealing with changes in the standard library and how to fix them or detect them before compiling I didn't know Difficult question. I don't have really an answer to that. I don't know Any other question? Hi. Yeah, I just wanted to say I don't want circumstances. Do you think you might look to migrate from Python to go? What kind of is it for instance if you are on the Python? To version and you are looking at implementing some of the concurrency features of Python 3 is it do you think it's Worth looking to go as an alternative to getting used to the whole Concurrency syntax that you've got in Python 3 or do you think that that kind of leap is not really worth doing? Well, if you I think that if you are happy with your Python code and it works Okay, and you don't have any bottlenecks then of course you don't need go or anything else But if you have any problems, then why not to try go, right? So I mean you must have I probably missed the first part of this. I'm sorry, but you how did you get into go? So what what drove you to to move towards go and obviously kind of get experience? Yeah, we okay. We We had a in our company. We had a service in Python which Wasn't working really good. Not because it was written badly or something just it just Couldn't process Process the data the amount of data we had so we decided to try it go and It's working really good right now and This is we're real happy with this choice we are I think that we will We will be rewriting our services and go Thanks very much. I wanted to ask What is the What would be an application that you think should not be written in go that should be written in Python like what would be a good example of an app that Python would be better. Mm-hmm. I don't think there is this kind of application that shouldn't be ported to go. I Can't think of a way Why You can you can put everything in go? I mean the point of putting something to go is only if you need something to make faster or if you if you really want to benefit from Concurrency because in Python concurrency is hard from my point of view That's it Okay, thank you. We are running out of time. So if you have any more questions, you can meet max outside or open an open piece Thank you