Check out a free preview of the full Go for JavaScript Developers course:
The "Printing with fmt" 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 navigates to the Go playground and explains how to print to standard output.

Get Unlimited Access Now

Transcript from the "Printing with fmt" Lesson

>> So far we've seen the thumped print line function kind of a lot as we're printing to the console. But that's not the only formatted string function that we're going to use today, and kind of the patterns and similarities between them are a common thread throughout go. So I wanna talk a little bit about what those look like.

[00:00:16] We're gonna hop over to the Golang playground, just to see what that looks like in case any of you want to stay there for the day. And we're going to experiment with some of the other printing patterns. So let's hop over to PLAY.GOLANG.ORG in your browser. So when you open it up, you're gonna see a yellow screen that looks like this.

[00:00:39] But, you'll notice that you see a copy of the file we just created with that package main, func main, and some stuff like that. And so within here, we can hit Run at the top and it's gonna run all of your code similar to how we just did locally.

[00:00:51] But what I wanna talk about is this thumped print line function. So, right now you're seeing the console log equivalent of getting stuff displaying in your console here. The other kind of ways to print include Thumped.print without the line. And if I run this, you'll see I have two lines that look pretty similar.

[00:01:16] If I were to copy that and do that a second time And hit Run, you'll notice that there's no new line attached to the end of that console log. So the difference between printline and print is that you have to manually insert a new line if you so choose.

[00:01:36] And the reason why this is important is because, as we're actually using some of this formatting to return an actual string that we want to keep, this formatting is really helpful for what you actually want that string to look like. So print line inserts a new line, print itself does not, unless we actually escape a newline character at the end of our string, which we can do.

[00:01:55] And then the third version that we're going to be using a lot throughout the day is Printf. And Printf takes two or more arguments. The first is the string you want to print, and the second is comma separated, and it's going to be a list of arguments you're going to replace within that string.

[00:02:14] So right now, there are no variables we're replacing, but how this would work is in the actual string. Let's say this was the variable that's coming in from somewhere else, I would replace it with a string verb. So here percent s indicates that what I'm replacing it with is going to be a string, and this is a common syntax that comes from C.

[00:02:35] And then on the right hand side of the comma separated values, you're gonna have the same order of the actual values you want to replace that string with. So here we've got, hello, my name is percent S. And then that first argument it encounters has to match the type, which is a string, and it'll interpolate that into our string on the left hand side.

[00:02:53] So if I run this, you'll see that this string appears down at the bottom. Similarly, there are a few different types of verbs. Let's see. It is September 25th, 24th [LAUGH] Thank you. So September 24th, this year, is technically an integer in my string. So, to indicate an integer, it's %d, and so now I've got two different verbs.

[00:03:24] And on the right-hand side, the comma separated values are going to go in order of the way they appear. So if I want that second argument to be an integer, we would have 24. And you'll see all of those verbs get interpolated into our string. And so this is gonna be a pattern that we actually use pretty regularly throughout the day, but those are the three different print versions and all of these are going to display in your console, which is different from a couple of the other functions we're gonna talk about next.

[00:03:52] So that top section are the three that we just covered, print, print, line, and print f. And these all print output to the actual standard output console, as you're seeing that display kind of like console.log. The two others that will be pretty common are Fprint. And this is going to print the output to an external source.

[00:04:09] Let's say we want to render text to a browser, or save it to a file instead of sending it to the console, you'd use thumbed Fprint. But similarly, those three patterns, the print, print line and print f, all work the same way. So that pattern of either new line, no new line, or we're gonna interpolate some string verbs, will all behave similarly for all of these patterns.

[00:04:28] And then the last, the sprint or Sprint, is going to store the output on a character buffer, which means you wanna save that string to a variable. So when we're talking about how font.printline returns a series of bytes, Sprint returns the actual string you're trying to talk about.

[00:04:42] So this is why it's kind of good to keep an eye on what we're trying to do, what are you trying to do with that string, and what happens next with that information.