DEV Community

Cover image for Go Course: String Formatting
Karan Pratap Singh
Karan Pratap Singh

Posted on • Originally published at karanpratapsingh.com

Go Course: String Formatting

In this tutorial, we will learn about string formatting or sometimes also known as templating.

fmt package contains lots of functions. So to save time, we will discuss the most frequently used functions. Let's start with fmt.Print inside our main function.

...

fmt.Print("What", "is", "your", "name?")
fmt.Print("My", "name", "is", "golang")
...
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
Whatisyourname?Mynameisgolang
Enter fullscreen mode Exit fullscreen mode

As we can see, Print does not format anything, it simply takes a string and prints it.

Next, we have Println which is the same as Print but it adds a new line at the end and also inserts space between the arguments.

...

fmt.Println("What", "is", "your", "name?")
fmt.Println("My", "name", "is", "golang")
...
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
What is your name?
My name is golang
Enter fullscreen mode Exit fullscreen mode

That's much better!

Next, we have Printf also known as "Print Formatter", which allows us to format numbers, strings, booleans, and much more.

Let's look at an example.

...
name := "golang"

fmt.Println("What is your name?")
fmt.Printf("My name is %s", name)
...
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
What is your name?
My name is golang
Enter fullscreen mode Exit fullscreen mode

As we can see that %s was substituted with our name variable.

But the question is what is %s and what does it mean?

So, these are called annotation verbs and they tell the function how to format the arguments. We can control things like width, types, and precision with these and there are lots of them. Here's a cheatsheet.

Now, let's quickly look at some more examples. Here we will try to calculate a percentage and print it to the console.

...
percent := (3/5) * 100
fmt.Printf("%f", percent)
...
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
58.181818
Enter fullscreen mode Exit fullscreen mode

Let's say we want just 58.18 which is 2 points precision, we can do that as well by using .2f

Also, to add an actual percent sign, we will need to escape it.

...
percent := (3/5) * 100
fmt.Printf("%.2f %%", percent)
...
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
58.18 %
Enter fullscreen mode Exit fullscreen mode

This brings us to Sprint, Sprintln, and Sprintf. These are basically the same as the print functions, the only difference being they return the string instead of printing it.

Let's take a look at an example.

...
s := fmt.Sprintf("hex:%x bin:%b", 10 ,10)
fmt.Println(s)
...
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
hex:a bin:1010
Enter fullscreen mode Exit fullscreen mode

So, as we can see Sprintf formats our integer as hex or binary and returns it as a string.

Lastly, we have multiline string literals, which can be used like this.

...
msg := `
Hello from
multiline
`

fmt.Println(msg)
...
Enter fullscreen mode Exit fullscreen mode

Great! But this is just the tip of the iceberg...so make sure to checkout the go doc for fmt package.

For those who are coming from C/C++ background, this should feel natural, but if you're coming from, let's say Python or JavaScript, this might be a little strange at first. But it is very powerful and you'll see this functionality used quite extensively.


This article is part of my open source Go Course available on Github.

GitHub logo karanpratapsingh / learn-go

Master the fundamentals and advanced features of the Go programming language

Learn Go

Hey, welcome to the course, and thanks for learning Go. I hope this course provides a great learning experience.

This course is also available on my website and as an ebook on leanpub. Please leave a ⭐ as motivation if this was helpful!

Table of contents

What is Go?

Go (also known as Golang) is a programming language developed at Google in 2007 and open-sourced in 2009.

It focuses on simplicity, reliability, and efficiency. It was designed to combine the efficacy, speed…




Top comments (0)