 I'm going to give a little presentation about GoLang for Pythonistas and so I'm Alexandre Bourget, I work at plot.leadplot.ly, we do graphs and I've always wanted to have like a statically typed language under my belt, right? I didn't want that to be CC++, especially not for networked programs or you need to build the whole thing and learn all the humongous craft accumulated in the last 30 years. So when I heard about Go, I was so it was like Python. It was beautiful, elegant, you had some sort of the same productivity, readability and ease of use. So I had a hard time believing it, but then I tried, I thought I do due diligence and tried myself. Now today I think I could say that Go is the perfect companion to Python, right? Well you can have statically dynamically typed languages and like attack any task. So today's going to be a live demo. I'm going to be here in there and we're going to create MTL Python 49 and I use Emacs, okay? And if you, sorry? Which one? Why? Why Emacs? We can have a chat about that later. So this is my Emacs session. You've got the goofers down there. So every package starts with package and we have a function that builds into an executable. Okay? If you have that package main, we'll build an executable and I'm going to run here. I usually go build and then run the executable. Down here you see that? Is everything visible for everyone? First of all, raise your hand those who've written a small Go program already. And who would like to learn more about Go? Right, okay, cool. So that's what I'm going to run here each time, yes. Okay? And notice we'll get the output just underneath that line here for the rest. And so Go has awesome tooling. Notice I have Fly make that check syntax but it checks syntax more deeply than Python because you have static typing, right? And also it deals with import. So I'm going to run a command here that runs go FMT which formats automatically my code so I don't need to check PEP 8 and make sure I'm compliant. It'll do it for me. And it also, with a second, add the imports automatically, right? That's really cool. The tooling is amazing around Go. And so I'll define a variable here. So let's say A, my string and I'll add that here. Okay? This will show my string down here and this is the equivalent of having A which is my string again or something like that. Notice a couple of things. So comments are C style and we can avoid a lot of boilerplate because there's type inference. You don't need, like it's obviously a string so it'll be a string over there and after that you can't say it's something else like whoops, it's something else like that because you can't change the type, right? Variables are statically typed. Okay. There's awesome tooling also in the fact that you can like jump into the code. You'll read a lot more code because I have a special thing here. It'll jump to the definition of println down there. And whoop, I'm interested into std out, right? What is that thing? Oh cool. Oh, it's a new file. So a new file will give returns a file interesting and then, okay, it's a struct with a pointer to a file and finally that thing down there, private, blah, blah, holds that information. I have a clear picture of what that thing does now. I can easily go back and forth from my code to the standard library and just take note that we have a rich standard library just like Python so batteries included, right? So, okay. Now let's, in Python, we usually use different data structures to pass it around, right? Classes or anonymous dictionaries to which we stash a bunch of unstructured data, right? Until the time we realize that the dictas won't cut it, we need methods around and then we need to convert all those dicks into a class, right? And build all the class in it and all that. So, Go has another take on that and we'll define a little prefix, oops, sorry. So it could be overriding a native type. We'll have a special prefix type, can go around and know that needs to be a prefix but it's represented by a string or we'll have a little bit more complex and interesting what we call structs and these we'll have, for example, let's say a content for a prefix and perhaps a number of iterations, right? And I can create, so let me print output so that it's clear, oops, okay, where the output is. Over here I'm going to create a new prefixer and there's two ways. I can define the content by positional arguments, okay, let me make that mama prefix and then we'd have positionally also the end, so one time here. That'll work. The error I have is that it's declared but not used. Go would not tolerate that. We have things that are just dangling around without being used. So I'll print it out. So print f here so I can use formatting string, that's the equivalent to .format in Python. Prefix is, and then we have that special v there that will, like, write all the structure around all with all the definition. In fact, it is a pointer and all that. Yeah. So let's try the first one here with content. Okay, so what we see is that we have prefix content and the object is a string here and notice this is a pointer. For those who've made see something like that, it's automatically the reference. You don't need to do anything like boom, boom, to have a different syntax, right? You have a pointer or a standard object. Well, you use dot to the reference and it knows how to deal with the rest. How cool. And if I just print the whole thing so you see the definition, it's the main prefix object, the content is that and this and that. And I could, like, say, define also by name. So if I say this, is that clear here? The unspecified elements take the nil value for the type. So boolean will be false string and empty string and you can imagine the default null value for each type. Which is interesting, right? It's kind of a default init. But they're all there. Any questions up till there? That's a good question! Yeah! Okay, we have the answer to that. So you notice there's no init, right? What we can do if we want to do that, we'll create, for example, a new prefix that will return a prefix and then you do whatever it initializes. That's the idiomatic way to do things and go. And you often have a package that has that object and you call the package name dot new and it gives you a new object. So you can have many such initializers, right? But there's no standard one as a default except the convention new. Does that answer the question? Okay. So you notice also, no, you don't notice yet. So the way to add a method there, okay, let's just see that. That's an amazing one. So I'm going to run go fmt again, okay? And notice what's going to change here. Oh, have you seen that one? It is actually mixing tabs and spaces. How horrible! Only because you have awesome tooling. Can you do that? Where like you have things that are in my editor set to four and make sense. Whereas spaces are used for things that would not make sense with with tabs, right? But it's automated. So you don't need to bother with that. Yet I can change the awful indentation of default eight to four or two. And we don't need to stress that on people. It's just because we have tooling. It's so awesome that we can do that. And it makes sense. Okay, tabs and space. So we have, this is how we define a function. So prefix. That would, so the name would be fix. Input. And it would return, and let's say that thing would return just a fixed, like a fixed string, right? S print f, so it returns a string. We can see that by hitting cctl-cctl-d here. Right down, you have the definition. So we can easily know where you are. And that works in emacs, vim, and sublime. Because it's just external tooling that is called by that. So any editor can support that. And in command line, you could also extract that information. And many more. So we'll use the pref and the content plus the input. Make sense? Yes or no? Okay. And over here, we'll use fix with mama. So what we see is that it's prefixed. The string that we print here, fix returns indeed a string down there. You see the definition with the content of our mama prefix. Mama prefix boo. Got it? And the function here does that job. So see the pref is our self. The convention is not to use a self or this or what. It's to use something valuable in the context. So you can always know within a function what it is. And you see the return value string there is unnamed. And it makes it very fun. Whatever. Okay. So one thing when I came to go was that I felt really awkward. There was no exceptions. And I feared that, right? Seventy styles like error handling where you return statically typed. So you return a value that actually could be a valid value in the context. But you need to check that it's not an error. And if you fail to do that, you'll have like tremendous flaws. It's very difficult to track down. So Python solves that with exceptions. You cut the flow and then return. Yet most of my programs, I don't know yours, but crash because of uncaught exceptions, right? Yeah. Well, how does it crash? Seg faults? No, not real. So goal I take on that is to have multi-valued returns. And it's actually pretty slick. Even so sometimes it can be fearful that you don't have exceptions. But they have a good point, I think. Not that exceptions are bad, but at the same time it feels more robust when we do things this way. So I'm going to write a little function there. Crack it. And this one is going to crack my system using the latest hard bleed, poodle, shell shock, and what have you. And we're going to use here the read file, to read the password file, right? And the idiomatic way to do that would be to check the error there. If you look at the definition, look down low and then little, it's undefined. So what do we do? Go fmt. Woo-hoo. And it's up there. See? io slash, it detects finds in the installed packages. Okay. So we have that. And so read file takes one string, returns a series of bytes, and this unicode support in Go is really cool. It's the same guy who wrote unicode that wrote Golang, right? So it should be. So you return a list of bytes and an error. The error can be nil, which is the null value. So what we'll do is, if error unequals nil, then we'll return ourselves, like if bubble it back, right? And we'll format that. I don't know. We can say it's bleeding. No. It's impossible. I don't know. And then we put the error in there. So will that work? Oh, yeah. Not enough arguments. So we need to return two things. So in fact, it wasn't cracked, right? I couldn't crack it. So I would crack it here. We won't do that today. And return true with a nil. So that means the error is passed there. So the next one can do the same if there was an error up there. It kind of forces you to deal with errors. So how we would use that, we'd say crack it. So cracked and error. Oops. We'll define these. And we'd in there. So let's say fmt print, alana, cracked. No. Will that work? Yeah. Come on. Pass the declare not use. Cool. We'll get back to that. So we need to do something. Yes. You have chocolate already. Yes. Go ahead. Yeah. So what we can do, first of all, here we want a silence password somehow. But what we can do is explicitly silence error. And say we don't mind, right? But you need to do that, the return error. You could just not take the return value, but you'll need to silence it somehow if you want to get the return value. We'll do that here with password because we don't want to have it in our, so the underscore is just like we've seen just earlier with the previous presentation, it silences it. Okay. So what we get is nil true return cracked. So and the error was nil. You see that here? The error was nil. And it was cracked. Yes. Because it went through our code. So let's change that to password. What we get now is false. There should have been put together before we have impossible and the reason why it's broken. So we kind of bubble up the errors, make it a string that's still comprehensible up there. That's the way you hand our errors. Are there any questions? Yes. You need to return two values. Yeah. So false will be the Boolean value up there. No. So Booleans, yeah, unless you have pointers to a pointer can be a nil. Otherwise everything is mostly passed by value unless you have a pointer to point to different stuff. Yeah. Yep. Yep. Okay. Now I wouldn't be a good Golang presenter if I didn't talk a little bit about channels, right? Because all the awesome concurrency comes from the channels in there. And Go will say, like, don't communicate by sharing memory and handle although mutex and locks, rather share memory by communicating. And channels are the implementation of that. They're similar to queues and there's also in Go routines, which we'll see, that are similar to G events, threadless, greenless or some sort of concurrency stuff we have in the Python world. Except this goes 100% of all your cores. So there's no gil, right? And the go runtime manages system thread underneath to make sure it can go across all your CPUs. But so there are certain thread safety. But if you use channel to communicate, then you can shield yourself of all those problems. It's pretty cool. Okay. So we're going to create a function called flying mama. Okay. And it's going to receive as a parameter a channel. So it receives as a parameter like a pipe, like a metal pipe. And then you can put something. I have a copy of it. And from there we'll sleep for a second. Right. But I don't have time in here. So what do I do? Kaboom. And I have time up here. Okay. Also works for removals, right? Oh, it's not there anymore. Okay. So and then what I'll do is I will send a value down the pipe. And to do that I use a little like arrow thing. That'll send a value inside the pipe. So how do I call that? I'll call with, let's say I'll define a pipe. So make creates allocates memory. I don't know if you know it is up till there. We didn't malloc or free and blah, blah things, right? Because Go has a garbage collector, which is cool, right? Nowadays, I think we can afford for at least network things like it's really developed by Google for large network systems. So it made a lot of sense to have garbage collector. So you won't use that for real time systems, like in a hardware that needs to have very precise control, but makes a lot of sense for a lot of things. Shan, and that'll be a queue with five spaces. Okay. It's a buffered queue. And then we'll call flying mama with that pipe. And after that, what we can do is print like waiting for mama, oops, and got the mama over here and will read from the pipe. So what if I run that? What happens? You've seen, I'll run it again, runs output here, then I'm going to go up there, see it was sleep, oops, sleep, and then send the value in over here. It blocks because, why does it block? Yeah, because it sleeps, right? It blocks. So what I'll do is I'll make that a go routine. So it won't block there anymore. It'll send that in the cyber ethereal like space. And then it'll follow through to this one. It'll block here because it's going to wait for a read. So if I run that, like I'm waiting for mama and then I get the mama later. How cool. Get the mama. Okay. So maybe you notice when you get the mama here, it's type value. It's a Boolean. So I can, if I tweak this one, make it a false and you'll see that we'll receive a false root. But nothing prevents me from making that a prefix or whatever other data structure, right? And from here, we'll send a prefix with nothing in there. I don't know. So and what we get down here is our actual thing, the prefix object. Have a question? You send the pointer, the pointer is sent. If you send a copy of the thing, the copy is sent. So you can send pointers. So both ends could modify these. So in some situations, you might want to be aware. But what you'll do usually, you'll funnel all your changes to the same things to the same like little go routine that manipulate. So it's never run by other pieces of code. You can know that it's going to be running in one code block. Yes. So it intertwines OS threads. It manages OS threads underneath, right? And it abstracts that the run go runtime manages that for you. So the system threads are not exposed. That's a good question. Sorry. Okay. So there's also awesome tools. And what we would be a presentation without Docker, right? So we need to have a little bit of Docker. And what would be a language if you couldn't compile it to 32-bit windows, right? Or Windows 3.1 for work groups. So you can do here Docker run T for terminal I for interactive and V. I'm going to share the local directory here with the go pass inside a little image that Google built to be able. And I'm going to share the R. I want to build R a little tool here. So it's go pass and SRC and GitHub, and in there. So I want to use the container windows, which is pre-made to cross compile to Windows and run go. Shoot. What was that? Go lang windows. Not defined. Whoa. Oh, run. Sorry. Crazy. Okay. And what do we have here? mtl.exe. Whoa. And you might have noticed here, well, first of all, run that, man. See, we get, like, with Rewind, we can get it. And notice that we build one single executable. And we can deploy that this way. And it's possible to have completely statically linked and put that in a Docker container that weighs five megabytes. So you don't send the full distribution. You ship only your superb go code, completely isolated. Very easy to ship. And also, what would be a language if you couldn't run it in a browser, right? So go for JS. Does that for you? It really compiles go code to browser JavaScript. And you can have bidirectional. So if you have, like, image manipulation, you want to also run it in a browser to do stuff, you can keep your go code and run it there. How freak? That's it.