Go commands

Todd McLeod
A free video tutorial from Todd McLeod
Tenured College Professor
4.5 instructor rating • 19 courses • 308,178 students

Learn more from the full course

Learn How To Code: Google's Go (golang) Programming Language

The Ultimate Comprehensive Course - Perfect for Both Beginners and Experienced Developers

46:34:12 of on-demand video • Updated August 2019

  • The ultimate comprehensive course
  • For beginners and experienced devs
  • Taught by a university professor
  • From beginning to advanced concepts
  • Concurrency, channels, benchmarking
  • Testing, error handling, documentation
  • Hands-on exercises with solutions
  • Access to valuable code base
  • This course is tried, tested, and proven
  • Over 2.65 Million students taught
  • Lifetime course access
  • Learn at your own pace
  • 100% satisfaction guaranteed
English [Auto] All right it is two o'clock in the afternoon. And dogs decided to get up and get going with their day. I just got done with an awesome lunch pizza of course coder's like staple food. And I had some rice with garbanzo beans and some ice cream and vitamins A couple of vitamins. So now I'm ready to get back and get doing good doing good doing stuff going on the stuff I want to get through all of this stuff. Here we have a pretty big section and we're right here with go commands I want go commands get her get her a cross-compile and package's want to knock all those out before three o'clock. I've got a meeting at 3 o'clock. Cool. So now we're going to look at go commands and to do that I'm going to start my editor go land my ID and so that's the X-C. I don't need that anymore. This is the one I want. I could right click this and choose pen to taskbar. And now I've got a nice little icon down there which I like. I'm going to click that and I'm going to go into my downloads folder that's opening up and I'm going to get rid of everything in there. Don't eat it. So I no longer have that X-C. And this one here this has gone this temp one that's what caused all those problems for me in that last video. So I mean delete that one. So that's gone. And now I'm ready to open a project and I can go and I can look inside my go workspace inside source. And here's that namespace thing I've got and I've got my name goes to Levin and I have a couple of different repots there. And so going training was the original course repot a couple of years ago two and a half years ago. This is the go webdav one. This is the one I want to work in. And there's a couple of people who are recreating this course in different languages. So that's why you see in Japan Italian is Spanish. They all dropped off I don't know what happened but I'm just going to create a new folder in here 12. And that folder will be for everything that we do in this update. So there's a temp folder maybe maybe I'll create like hello world. All just go with 0 12. Because a lot of this stuff has already been written for another recording so I'll stay the same but was 0 12 so I'm going to open up codes go going go programming that's what I want to open and then I'll expand this window. And then inside here I have this is the English version Section 2. I don't know what they are doing with that and I'll create a new folder a new directory and we'll just call this 0 12. I don't know what to call it you know. Updated version. So everything we're doing an update or will be inside there. So we're going to learn some go commands in this video and go commands will. We already saw a go version so I'm going to bring up my bash so I'll come down here and get my bash on Windows and bring up my Bash now with my bash. I'm going to do go version. We already saw that print out the version and we can do going and we print out our environment variables and we can do. Go help. Print out all the go commands. Right. So here are the commands right there. And and then also additional help topics that we could we could print out so we can do. Go Help topic or go help command to get those Setsuko go help. There's also Funt go fund and we have fun right here. And run go Fonte on package sources how you learn about that go help fund and so forth. There's the format command and there's the format package two different formats but same name. And so the phone runs the command go from dash dash W on the packages named by the import pass that prints the names of the files that are modified. So I can use go funk to automatically format my code and I'm just going to show you some stuff here. So this is kind of like a tour. And for people who have more experience you'll like pick it up and be able to run with it. But if you're just getting started just kind of take this as like hey welcome to Disneyland. Let me walk here around and show you some of the highlights. And then once we're done see the highlights we'll go back and we'll explore everything I showed you in more depth and actually figure out how it runs. So it's just kind of like I like sit back in the convertible and enjoy the ride watch the sights. We're going to start back and visit everything we see here in more detail in depth throughout the course. So just totally low pressure. Don't worry about whether you understand everything or not. I'm going to do a one in there and just do hello world just to recreate it and inside Hello world. All right click and choose and make a new go file and my naming convention by the way is completely just for the purposes of a class to keep things in sequential order like this is a sequence we learn things and you can move through it in a linear fashion as opposed to jumping all around whereas stuff. So that's the code organization that makes sense in a class in a programming project. You'll you'll have different code organization and do you want to add the following file to get. Remember don't ask. Yes. So the entry point for all of your programs is package main and you have to enter func main and so package making func main That's the entry point of your program and it's also the exit point when you exit func main Your program's done and you can see my imports came in really well there. So now I'm perfectly right. So now that's that's not formatted correctly according to. Not conviction but Convention. It's convention for formatting my dogs now come in to say hello. That's not formatted correctly but if I go to that file so right now print working directory I'm there I can CD into documents go workspace source get Herb goes to 11 go Layng and which is it. It is go training going webdav. What is this Ribault called this repo is called and goes to 11 repositories going webdav go programming. Just go programming. So there you go programming. And now I'm in there. And once inside there I could do and class and I could see all the folders and I could go and see the code samples and then into 0 1 2 and 0 0 1 and then and there. I can do go fast and it's going to format that code right. So go find Main not go got formatted and when I click in here this updates right so it's automatically update about 4 go lammed it doesn't change until it has focus. So just formatted that code. Now having standardize the convention of standardized formatting makes the code easier to read between team members because everybody's code is going to look the same. If I wanted to I could do go Fonte and this triple dot like that dot forge slash triple dot and format everything and down through the directory. So if I came back up to the root of this repository will come up a little bit and see we're at we're right now in the route of go programming. And I do go fucked dot for a slash triple dot and syntax unique to the GO command. It's going to format everything in this repo. And so now it's formatting all of those files and making them all look the same as a good thing we're doing this cuz seems like some of them needed some formatting All right. So we got that formatted and that's go fun. And that's the main thing that you want to see with that go. And then this deal here. The next thing to learn about the GO command is we have go run go build and go install. And so just take a quick gander at what those do. So I'm going to once again go back down into a CD and I need to go into code samples and then 0 0 0 1 2 0 0 1 and 0 0 and then that's it. And then last and where it mean dog go. And so if I just go run this runs it go run maned go. There we go. Iran right. Hello. So that ran. So that's perfect. And go run we'll build and run your code and then it gets rid of any of the files. And if we go look at this file right here in our workspace so that's in the documents go workspace and then we have our binary. There's no binary in there. OK. So go run. Just ran it. If I do go build this we'll build it and they'll drop an executable right into my folder. So go build is kind of good just for checking to make sure it can build and you can also run it from there. And so now I have an executable right there and right from here I don't know if this will work on Windows. Yeah totally it works. So that Daut forward slash means right here you'll find this file right here in this folder the dot often means right here right here in this directory for Slash you'll find that file and execute it so that till ran. And if I come back here there's nothing in this binary folder. Now if I do go install go in. Well that's not where I want to be. If I do go install may not go right. Let me get rid of this. So that was already gone boom that's gone. But what it did now is it put it up here. Well where has it been binary package been binary package source. Where does it go install and do it again. Oh no install location for go files lists on command line go Ben nots. Wow that's interesting. No install location for go file. So it's like it can go EMV and go in the house go bad and it's not set for anything. I didn't realize I had to set this. I've never had to set it before but apparently on Windows you do. So I'm going to set. Go Ben. How do we set that. Here's the Ben. So I'm going to copy that control see. And then I'm going to go back to right here and right click my computer and choose Properties and then go to advance system settings. Environment variables and new. And I'm going to call this an Gobin. It just sounds kind of cool and hit. OK put in that path to that bin and and then hit OK and OK on this okay on that and close this and then restart this and say I'll type Bash and restarting reloads all the environment variables and yes there are other ways you can do it but that's good enough. And now if I do go in vi my go Ben is set to that location. So if I go back to see the documents go workspace source Herb goes to 11 and go dash and then 0 0 12 0 12. Come on baby code samples 0 12 and 0 0 1. Now if I look in there there's me and I go if I do go install. And you can do Stardock go to get all files with a go extension but there's only one so I could specify it. Mean go and record it. Dropped it in right there. So there's my main go. And if I'd set a path variable to that location if I'd set a path variable to that location then I could just run that by typing main X-C and it would run. So just out of kind of interest rate I can do. So let's go back 1 and let's get that then. Well maybe because we said Go bed and it'll still even work. Maybe you don't. Sometimes things change. All right. Command not found right. But now I'm going to go into properties. An advance system settings and environment variables. And then here's the path and I'm going to choose add it and choose new and just drop that path then and hit. OK so that control controlled to pace that and then close all this and then reload the terminal and then and then just type in main X-Sea and now is going to look down all those path variables there may not be see here no as their main idea here though is there may not be some here yeah right here. You want to run it. We sure do. OK we're on it. Hello. Right. So that's what that path did it just looked down those paths totally cool. All right. So that's see if we covered everything. Go run go build. Go install. And then you can also do flags like the dash race flag and we'll learn about that when we jump in on concurrency. So go run needs file name go run may not go. E.g. go run mean dog go and go run file name or go around start and go go build for an executable. Builds a file reports heir's of any if there are no errors it puts an executable into the current folder and for a package it builds the file and reports errors if any throws away the buying areas that's go build and then go install gives you an executable compiles the program names executable the folder name holding the code. In our instance it named the Mac it names the folder name but names executable Mac the folder name holding the code windows and do message me if there's something different there. Maybe I got that wrong Mac. Windows it's the file name puts executable in the bed and then for a package compiles the package puts the executable in the workspace package. So if we have packages we would have had things show up here in PKC but there's nothing there so we don't have any. Any third party packages. So that's that's working with go commands. And again just a little tour of the countryside and we'll be stopping back through to visit these places in more depth. But if you have experience that probably makes some sense to you and you've got some good distinction over quite a few go commands at this point. Cheers saloon keeper. Keep up the good programming.