This course has been updated! We now recommend you take the Basics of Go course.
Transcript from the "Tools & Commands" Lesson
>> We're also talking about some of the go tooling around go commands, some of the toolkit that we've seen, and tools that are either built into the language or that we can go get from third party libraries. So the first thing I'd like to talk about is some of the Go tools.
[00:00:13] This template we've seen quite a few times, this is go run. And go run is the command that's actually building and compiling the code behind the scenes and letting us run it in the terminal. If you're to break up those commands independently, you could use go build as well, which will actually just kind of compile that into a binary that you can then test or deploy or do whatever you want to, you have a little localized version of that and it's much easier to get a feedback loop of what you just kind of put together without having to send it to any more complex process there.
[00:00:47] Go install, if you do have any third party libraries, will make sure that Go has access to all of that code. We haven't needed to use that for anything specific at the moment. But if you're inheriting a code base or working with libraries that you haven't, that aren't actually shipped with the Go source code, you would use fo install, kinda like NPM Install, to make sure all those are available.
[00:01:06] The go fmt command we've been using quite a bit, using the fmt library, but go fmt is actually a command you can run in your terminal as well. So if I hop over to the code here, I have a built in linter that's doing a lot of the work for me.
[00:01:19] But if I didn't, and I were to run, go fmt and then a file, a particular Go file, so if we did exercise_4c.go, and hit enter, it would actually print out the same kind of errors that we see in the linter as they're kind of happening on the screen.
[00:01:35] So if we want to try a Go format in the browser, my linter is kind of doing a lot of this for me but go fmt is actually going to kind of like clean up a lot of the spacing. So when we were in the Go playground and you saw that tabbing was kind of out of line and you hit that Format button, that's essentially what it's doing, it's kind of cleaning up your spacing and tabbing.
[00:01:52] And similarly, there's a command called go vet. And go vet, you'll notice that in this function average, I've removed the return statement that gives me back that total that our function is expecting. So if I run go vet, you'll see that that error message shows up in my console here.
[00:02:07] So I'm seeing that just scores are declared but not used. And this is helpful to have some of these commands run like either on Save or on Commit and having kind of like Commit hooks with your GitHub repose. Just to automate a lot of the stuff that you don't have to think about, at the end of the day before you deploy, what are the things you want to make sure are lined up in your Go code, and these tools give you a lot of power around that.
[00:02:28] So another command we can use to kind of list out any packages that we've established in our code base is the command go list. And right now I am in the top level of the directory. And because we're working in a code base, that's a little bit strangely structured, we have a lot of packages called main and a couple of tested files.
[00:02:45] I'm going to use the recursive syntax here, go list./..., to just kind of iterate through the directory and print out all of the packages that might exist within this directory. And so you'll notice when I do that, I'm actually just kind of seeing a series of file names where we have our Go files.
[00:03:02] And that's because a package essentially is a Go file that lives within a bigger folder. And so, as we structure the next package example, you'll kind of have a better example for how packages are written and organized. But for now, this command, go list is simply just like listing kind of NPM Packages, like what are all the packages that I have access to in this application?
[00:03:22] And then the last few that we've kind of talked about a little bit, go doc. And then something like go doc fmt.Println will spit out that local documentation and let you kind of dig into examples of what a function signature looks like and additional details, go get is going to be similar to an NPM install, it's going to go fetch a third party package and then install it locally with the rest of your Go directories.
[00:03:48] So the example here on the screen is it'd be fetching a package called golint, which is an additional set of linting rules that you can use, similar to go vet and go format, that you can run on Commit or on Save, etc to make sure that your Gk code is up to par.
[00:04:07] And then to actually run that, that last command there is golint, because this is a third party you don't use just the go and then the next command you would use go lint and then run it in the directory.