Check out a free preview of the full Go for JavaScript Developers course:
The "Printing Exercise" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna demonstrates how to access the local Go documentation from the terminal, and introduces the exercise on printing.

Get Unlimited Access Now

Transcript from the "Printing Exercise" Lesson

>> The point of this exercise is to practice with a little bit of string interpolation and also Hello World and kind of getting used to that file structure. One thing I want to introduce before we get too far into that is back in the terminal And I'm just going to change into.

[00:00:19] The directory here. Bump up the font for you. So Go also comes with, besides we've run go run and then a filename, we can also access the local documentation, which is essentially the exact same thing you saw when you're looking at that packages documentation in So if I run go doc and then whatever I'm trying to learn about, it's gonna print out essentially the same thing you just saw in your browser, which is super great, especially if you're on an airplane and you don't know what's happening.

[00:00:51] But for instance, if we wanna know, okay, I've been using this f print, I'm not quite sure what it returns. I don't know what this is. If you run go doc FMT and then like print line for instance, it's gonna spit out exactly what that function does. And what's great about the documentation and we'll talk about why this function signature looks the way it does.

[00:01:11] It'll tell you what the function is, what are arguments it expects and what it's going to return. And one of the best things about Go is that these are all things that need to be addressed. You can't just leave them blank, you can't ignore them. And so understanding where to go to find the information if you're missing it is incredibly helpful.

[00:01:28] Cool so let's go back to exercise 2a, and that'll be in the 02 introduction section here and exercise 2a talks about the following. The goals like I said, are to use the font to package and practice some string formatting. We're gonna create a file called exercise 2a.go and you can put this in the code directory.

[00:01:52] Each of the sections has a folder already made called code. If you just want to throw all your go code in there, and it's also kind of helps alleviate that problem with having too many main packages hanging out, just kind of keeping everything a little bit more isolated.

[00:02:03] You're also welcome to use the go playground. If you'd rather not create files for a little while, that's fine too. And we're gonna start with the following code which is just that base template for a Go file, with the package main, import prompt, and the func main. And within that, we're gonna practice the following exercises.

[00:02:21] First of all, just kind of re experience the hello world example. So we'll just print hello world to the console. And then after that, we're gonna do some more printing. So the sentence I'd like you to end up with is something like Hi, my name is Brenna. I've lived in Denver for four years.

[00:02:38] They say the weather is amazing, which is true. So you'll notice that there are kind of three different types that we're working with in there. We've got a couple strings for name and hometown, a number for how long you've lived wherever you live. And then a true or false for whether or not the weather in that location is amazing.

[00:02:52] And if you need help, definitely dig into the documentation around string formatting, which you'll find at the top of the thumped package information. And then in about three or four minutes, we'll circle back and talk about the solution.