Developing Go on Windows with WSL2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the channel uh in this video what i want to do is i want to show you my preferred development setup before go using windows so we're going to walk through a setting where we can use windows subsystem 2 for full linux on top of your windows environment to get the best possible go developers experience we're going to look at things like vs code and some couple of extensions and we'll just kind of walk through the the process i'm going to end the video with a a little kind of example code just test that things are working and for folks who are not familiar with go it'll give you a little bit of a starter but this video is not intended to be a an all comprehensive how to program go on windows it's just about setting up your development environment so let's go so what i've got here is a fresh install of windows and i'm actually running this on a virtual machine on my main desktop so i can go through these these settings but what we have is is not a massively powerful machine so the the machine i'm running is set to about four cores or something like that and about eight gig of ram so we're going to go through we're going to click on all of the various different settings that you need to run windows subsystem for linux so firstly i suppose why use windows subsystem for linux why not just develop go directly on your windows machine and i think the key thing for me comes down to tooling so for me i'm a distributed systems developer i look at things all of the time which have revolved around sort of linux based servers so having my development environment and using the same tooling that i'm going to use on my deployed environment just makes things absolutely easy i think also there's a bunch of tooling which is really beneficial from from linux i mean simple things like using curl gpg but you can still take advantage of windows you can still run all your gui you can run all your games and things like that and the two can sit completely side by side so windows subsystem for linux allows us to run linux on top of our windows environment and it's going to run in in a light vm so it's it's not going to be that too bad in terms of the overhead and things like that and to set this up there's quite a few steps but once you get going it's it's pretty easy so step one we're going to install wsl 2 so the latest version of windows subsystem for linux we're going to configure the terminal and we're going to basically just get it get it up and running now this process does require that you've got a relatively patched version new patched version of windows i think anything that's got the the most sort of comprehensive half yearly build you should be good and it's going to require a couple of restarts as well so let's begin now windows subsystem for linux says actually when you look at the documentation here and i'll put all the links down below there is that you can see here that it's got a simplified install now this is a preview release and this is supposed a thing to come now microsoft's investing quite heavily on development of linux with with windows so we're going to see some some benefits in this process moving forward but for now we're going to use the manual steps in this document so let's have a look at this so the first thing we need to do is we need to enable some some features of windows so we need to install the windows subsystem for linux feature and we can do that using powershell so let's go over here and grab a powershell window now the important thing with this powershell window is that you can't just use the ordinary one like i've just opened here what i need is a administrator version so i'm going to go over here back over to my my powershell i'm going to write there and then you see now i have run as an administrator so this is the version that i need to use it's going to say hey do you want to allow this app to make changes to your device i'm going to say yes there we go let's just zoom that up a little bit so we're just literally going to go through this microsoft document and we're going to check off all of these features i'm going to copy that command we're going to go over to our powershell and we're going to paste that in so you can see that that is installing and it's enabling that feature now to progress what we need to do is we need to restart our windows machine and we are going to need to restart a couple of times so let's go do that now click here power and restart and there we go so windows has just restarted there so now let's move on to the next step let me just open up my browser again and back to our settings page so the next step is that once once we've got the windows subsystem for linux enabled we can check some requirements now i know this version of windows is good but you might want to go through this step and check those things i'm just going to go straight on to step 3 and i'm going to enable the virtual machine platform for windows so again i'm creating that powershell command so i need to go into powershell i need to run it as an administrator just choosing yes there paste that command in there and then we're going to run that so that's completed successfully again if you go back to the dock you can see that we need to restart the machine continue with that wsl install so let's go back power again power again and restart okay so that's just restarted so again let's go back over to our document and we're going to say yes please thank you very much for reopening this all right so we've enabled the virtual machine features now what we need to do to to use wsl 2 is we need to install the the latest version of the wsl2 kernel before we install any vm so again we can just click on this and we can download that package that's just going to download there 14 megabytes i'm going to open that file just installing it yes install through okay and that's set everything up so that's going to allow us to run the latest vms and to for our vms to be compatible with wsl 2. this was the recent changes i think about sort of six months ago or so this was updated so next step just before we install our linux distribution is what we want to do is we want to set our wsl default version so wsl the original version was obviously wsl1 and then wsl 2 came out which has slightly different architecture in some ways it's it's a lot quicker the things like disk access and things like that have been vastly improved over wsl1 and they're changing the network model ultimately wsl 2 is the version that microsoft are actively developing so we want to we want to use this version so again let's go over and create our powershell window run as administrator okay so i've now set the default version for any new virtual machines that i'm going to install as wsl version 2. so now what we can do is we can actually install our linux distribution so let's open up the microsoft store and let's install ubuntu so there's a number of different versions of linux that you can install i think um let's have a quick look actually see which ones they've got in the store here you've got ubuntu kali linux which is a security focus distribution sues uh debian you know there's a number of different versions i tend to to stick to to ubuntu predominantly because it's it's one of the sort of the most supported and it generally you find if there are packages for the various bits of software that you're going to be using you're going to find them on ubuntu we're not using any of the gui elements we're literally just using the terminal bits so let's use ubuntu so this the default ubuntu here is going to install ubuntu 2004 i think it is yeah 2004 lts which is the long term support version we're just going to click install here windows going to ask me if i want to sign in i don't on this machine because it's just a temporary vm but that's going to download ubuntu about 500 megabytes and it's going to install it while that's installing what i'm also going to install is the windows terminal so windows terminal is a is an optional step but it's a really nice application for managing sort of various terminal windows you can have tabbed versions of your ubuntu instance as well as your powershell i use this quite a bit so i'm going to install windows terminal too all right so ubuntu is just installed so we can click launch and then we can run it so it's going to do a final little bit of setup and i apologize for the terminal being a little bit small here but i can't actually zoom this in at the moment but it's basically what it's saying there is installing this will take a few minutes so it's now asking me for a new username so i'm going to use nick j um use whatever you want in there this is going to be your ubuntu username not your windows username and then it's going to ask me for a password um well i password i recommend putting a proper password in here but just for the simplicity i'm just going to use password don't do this at home all right so we've now got our ubuntu installed and everything's working there so let's close this window and then let's open up the new windows terminal that we just installed as well so this is the new windows terminal i'm currently using the powershell you can see that in the tab but if i click the drop down arrow here i can choose my ubuntu instance and there we go we've got our ubuntu instance now if you've not used linux again i'm not gonna run into how to to use linux in this but um i recommend you'll find a bunch of different um tutorials on on youtube for playing around and i recommend learning the sort of the basics of that but just as a kind of a couple of very very quick things what we're going to do is we're going to change into the linux's home folder so we're going to do that with cd dollar home so anything prefixing a word with a dollar is going to be an environment variable in linux so we do home and then we can do ls ella which is the same as dir and then we do ls las which is going to give us a more detailed version of the directory and we can see there all of our files so now we've got windows installed what we're going to do is we're going to go ahead and we're going to install visual studio code i'm just going to download and we're going to install the stable build here so vs code works really well with wsl too you can use vs code on your windows desktop and it'll interface with wsl just for your files and any processes without you even realizing it's there so you get a really nice experience of keeping the windows ui that you're familiar with whilst being able to take the advantage of using the power of a linux subsystem so i'm just using all of the default options here uh yes i will so add to path we're going to register code as an editor for supported file types and add open to code with windows explorer and file explorer and just install that okay so now we can launch visual studio code and there we go so visual studio code is um is running there and you can see that it's oh if i just move my big head out of the way there's a pop-up window just on the the right-hand side just here which says uh you've installed windows subsystem for linux and stolen your do you want to install the rackomandx extension for it and the answer is yes you do so the windows substation for linux extension is going to allow you to open files in your linux vm from your windows desktop and it's going to allow you to do interactions such as debugging and things like that so if i go down here to the bottom right hand corner you can see that i can open a remote window and i can choose remote wsl new window and what this is actually going to do is this is going to open up a vs code window but it's going to be inside of our new wsl instance that's running there now we can open up a folder and let's just open up our hub folder home nick j and then all of this here is our files inside of homnikj so let me just zoom that up a little bit right so next steps what we need to do is we need to install go so let's do that now one of the things i really like about vs code is the fact that i can actually use the the integrated terminal so let's install go and let's use the the integrated terminal so i can just use terminal new terminal and then i get exactly the same terminal that i would have got if i was using my windows terminal so how do we install go so what we need to do is we're going to install go for linux and this is important because we don't want to install go on our windows desktop we want to install it for wsl so when you click on go install in the browser it's probably going to default to windows because it's detecting that you're using a windows operating system so we're just going to click linux there now what we need to do is we need to download the binary package for go and then we're going to run and install so let's download it so we're going to copy the link and i'm going to go over to my my vs code window i'm going to type clear now what clear's going gonna do is it just clears the everything that's in the terminal so far let's make that full screen and then what i can do is i can use a command on linux called wget and wget is just gonna download a file from the internet so we type wget and then the url that we've just pasted from our browser we're right-clicking that and you can see there that that's downloading it's about 100 megabytes or so again so clear clear the terminal ls and we can see there that we've now got this file so to work with a file and this is a compressed archive so it's a tar file what i can do is i can run the command tar extract zipped file and then i'm just going to give it the name of the archive so that will extract the the archive here and it will we'll just put it into the the current working folder now if we look at the go documentation what you can see here is that go is giving you a command which allows you to run everything all in one so i want to show you this way so we're going to use this method but in essence let's have a look at this it's exactly the same command and we'll break this down right first things first i've just pasted that in there and it's given me a whole load of permission denied now why is that the the reason is that what i've tried to do is if we look here let's look at this command and break it down bit by bit so we have rm so that's going to remove a file or a directory then we're using dash rf so that's going to be recursive and it's going to be force so i'm saying can you remove the file stored at the path user local go ampersand ampersand allows me to chain commands in linux and then i'm doing my tar i'm setting my output folder with this dash capital c to user local then i'm using the extract zipped file xzf and our archive so what's going to happen there is this command is going to delete anything that's stored and use the local go and it's then going to extract this gotar archive it user local now the tar archives root folder is is go so it will create a path use a local go now the problem with this is that even though i'm logged into linux permissions are ever so slightly different so by default user local is a special folder that i don't have automatic access to write to i can get around that by using the sudo command which is super user do so i'm going to do sudo rmrf now that's going to access it as an administrator or as root with the highest possible privileges and then i'm going to run sudo tar and that's going to do my extract so let's press enter it's asking me my password same one as i entered before which is password press enter that's going away in the background there and that's installing everything if we have a look at that folder so we can do ls dash las user and if i press tab it's going to complete for me use a local say go and you can see there this is where our go application is so can we run go right now well no and the reason for that is that go can't be found because it's not on the path so if we go back over to our go instructions and it says here that you need to set your path environment variable so the path is exactly the same as as it is in in windows but we're just going to do that in linux so we're going to set the path and the path environment variable on ubuntu goes in your file called profile you see this here so profile is in your home folder and what we can do is we can add simple bash commands which get executed whenever you create a new terminal so i'm just going to paste that in there and i'm setting this path so export path equals environment variable path so i'm basically appending to it the colon here is a separator for paths in linux and then use a local go bin and this is where the go executable file is so we'll save that now what we can do is let's open up a new terminal and now let's type go still can't be found why is that it's because we need to reload our profile so if we do source dot profile that's the linux command for reloading the the profile so now if i type go you can see there that everything's working because the profile that i've just changed isn't automatically reloaded we're going to use the source command to do that so that's go installed we've got linux installed for wsl2 we've got our vs code environment setup the next thing you do is let's write a very simple application now before we do that let's look at an extension which is going to make it easier for us the vs code has a number of different extensions and the go extension is one of them it's going to give us code formatting it's going to give us code complete and all sorts of fabulous things like that so i'm going to install that and that's all installed and then what we can do is go ahead and create a new application now go from about i think 115 114 maybe doesn't need a go path anymore originally you have to set the environment variable go path which pointed to the the source route for all of your go code and you don't need that anymore so let's go ahead and create some folder structure to contain our test application now what i do like to do is i like to use a structure like this which actually mimics the old go path so i'm going to create a folder and call it go inside go i'm going to create a new folder i'm calling it source and you don't need to set it up like this but this is the convention that i like to use folder inside there what we're going to do we're going to call this github dot com so the convention for gold packages is that you have things like github.com or gitlab or bitbucket or whatever you're using and then you have the organization name so my organization in github is nicholas jackson so let's create a nicholas jackson folder and finally let's create a folder for our application itself and we're just going to call this test app okay so we've got that folder created so let's navigate to that in our terminal so again ls and you can see there we've got this go folder oh we can actually already delete this archive as well so let's let's clean up after ourselves so rm go i'm just going to tab tab complete for me no you're not i don't want to there we go so we're going to clean that up change directory go let's have a look we've got source and we've got github.com and nicholas jackson and test app so there's no files in here so we want to create a new go project so what do we do so we need to use the command go mod init and that's going to create us a go mod file let's have a look at it here so everything in go is a module and modules are allowed how you kind of do things like track dependencies that your application may have it goes way of sort of doing package management as well so every application you create we're going to create a go mode now my module name is going to be the same as the source repository where i'm going to store this eventually which is github.com nicholas jackson test app and we're going to use go version 116. so that's created now for visual studio to be able to work with a go application i can't just open it up in a folder like this because the way that the the tooling works it won't allow me to to kind of the intellisense and things like that it won't be able to pick up the go mod what i need to do is i need to open up this test app in its own folder close remote connection create a new remote connection remote wsl new window let's open a folder home nick j go source github.com nicholas jackson test app and we'll open that up there and we can just close that one down so we've now got our go mod and this is all opened up inside of vs code this is now at the root so vs code is going to be able to do all of our intellisense for us so let's add a new file and we're going to create a very very simple little web application add new file and we're going to call this main.go which is just a convention now immediately you see here that we've just created a go file vs code is recognized and it said hey you don't have any of the tools do you want to install them so i'm just going to install all of the tools vs code is going to install things like a linters and stuff like that which will help me with the development of everything and you only need to do this the first time you open up everything but it's going to go away and that'll take just a couple of minutes to download those tools so that's everything installed what we can do is we can just close that window now go back to our main go code and we can start writing our application so let's just hide that terminal for the moment so the start of a go application is going to be package main every go file needs to have the package declaration and then we're going to do um create a function func main that's the main entry point for for our application so in this instance you can only have one funk main but that's all we need we just need the one so let's create a simple web server so a web server you can create and go by using the http package so it's http and handle funk now handle funk is a a way that we can add handlers to a path for the default default serve marks and again i don't want to go into too much depth on this i've got a series of videos which i'll i'll put a link up there too but we're going to go we're going to add that default func the path that we want to handle is slash and we're going to create a handler func and handler func has a syntax like this so it's funk and then we're going to use response writer and http dot response writer response writer is an interface and we're going to do the request which is http dot request and that's a reference so you can see we're using the the star there and finally we can just add some brackets onto that so to save that and everything is going to get formatted so let's just write out hello world all right so it's format dot f print now fprint is going to write a string to an i o writer now the writer we've got here is the http response writer which is the output from the web server so we can put that in there and then we specify our string hello world and we save that and that's our very basic http handler running so we can then create a web server to listen for that http listen and serve we're going to specify where it's going to listen to so we're going to use all ip addresses and we're going to say port 1990 and we need to specify the handler now if we put nil in here what go is going to use is is going to use the default handler now this handle func you just created there is on the default handler so that's all set up let's grab a terminal window and we can test this out new terminal let's have a look here we've got our main.go so we're going to do go run main.go and the application there is running let's go across and create a new terminal and then let's try this kill localhost 1990. i don't know why it's wrapping around there but it's probably because i'm zoomed in so far but you can see there that we get the output hello world so if we change this here to hello nick again save that file back over to our go server we're just going to control c to stop that and we're going to run it again with the go run main.go command back over to our other terminal let's curl it again and it says hello nick so that's in essence that's a very simple go program we've got that running on windows in visual studio but we're using wsl2 to actually execute our code so we get all of the benefits of linux but all of the benefits of windows as well now just very very quickly and this is because this is a question i get asked sort of quite a bit is if you're not used to go i recommend going through the the tutorial inside of the go website it's it's really good it'll give you an introduction into to go and it's syntax and the various other different things like that go packages are are sort of a concept that many new go developers have issues with so a go package is basically just a github repo or collection files and github repo that you import into your application so for example we've got our simple go application here what if we want to extend this to use an environment variable rather than it saying nick here so i can use a package the package that i'm going to use is a simple package that i've written called end and you can see end here it's just at github.com nicholas jackson so we're going to use this and we're going to just quickly get the environment variables i'm going to pop it into there so to import a package what we're going to do is we're going to do let's get our terminal i'm going to do go and we're going to do get and we're going to pass the url so we're going to do go get nicholas jackson end you can see there it's automatically discovered the semantic version 0.6 and it's imported that if you look at the go mod file you can see now that we have this require statement which is automatically added by by go so this is setting the dependencies and then in the go sum you've got some more detailed dependency structure so for example it's using things like go spew and testify somewhere along the lines my goend package will be pulling these in as dependencies as well so that's all there so let's add n into here so what we can do is we can define a variable bar name equals and we do n dot string so vs code is is completing this for us we're going to specify the name well i'm going to specify name and env is not imported so it's saying cannot find if i just hit save there you see that's going to bring that in and now we're getting all of the intellisense for that so the syntax is name which is the environment variable name if it's required a default value and a help string the name we're just specifying that's required and that's going to be false we want the default value and that's just going to be let's say nick and a help string so help is name of the person to say hello to and we can save that and then we're going to use this package and we're just going to do env dot pars so it's very similar to goes flag package and then we can just use that in here so we can say format dot f print so rather than format dot f print we can do format dot f print f and we're going to say hello percentage s and we're going to say name so we're saying hello and we're going to in put the variable name as a string and that's going to print that out there now why am i using a prefix of a star here it's using a d reference because if we actually look at end dot string it actually returns us to a reference to a string not an actual string value so we're using this star as a d reference so let's go back over to our go command kill our server let's run it again and this time we're going to say name so we can prefix environment as prefix at command and set environment variables in linux like this so using name equals youtube youtube youtube go run main so just for this go command we're now setting the environment variable name to youtube let's go over to our other terminal curl localhost and you can see there that we're getting hello youtube now the other thing that we can do because we're using wsl is even though that our application is running in linux we can actually run it over here in the browser so if i go localhost and if i use the keyboard correctly which is a long long straw i know it's a bit of a challenge but i zoom in there and you can see there it says hello youtube so we're just using localhost so wsl2 is automatically managing the the various different firewall things and proxying the application between our running go code and there are there and that's it so this is how i develop every day day in day out on on windows i use windows predominantly because i like to play video games i also use tooling like adobe premiere and things like that so wsl works really well i can have a single machine and i can have my linux environment running here in my linux terminal i can have all of my windows stuff here and i can use vs code as if it was a sort of a as a native application in windows but but also connecting up to visual studio code using the wsl extensions if you're interested to learn more about developing applications in go specifically around distributed systems then i will put a link there but please follow along on the go micro services playlist obviously if this has been useful to you please like and subscribe to the channel it helps us out a lot if you've got any comments leave them down below and i'll do my very very best to help you out but for now thank you so much for watching and i will see you next time
Info
Channel: Nic Jackson
Views: 20,730
Rating: undefined out of 5
Keywords: go, golang, wsl2, visual studio code
Id: poejKP1wTpc
Channel Id: undefined
Length: 35min 59sec (2159 seconds)
Published: Sun Apr 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.