Golang’s fmt.Sprintf() and Printf() Demystified

Golang’s fmt.Sprintf() and Printf() Demystified

Go has many Printf-like functions. To make life easier, I’ll simply refer to fmt.Sprintf() but most of the specifics will also apply to the other functions.

Go’s creators used the functionality of C’s printf and scanf as a basic template for fmt.Sprintf(). However, for non-C developers, the way C works may not be intuitive. Here, I’ll demystify Golang’s powerful fmt.Sprintf().

Note: Go has many Printf-like functions. To make life easier, I’ll simply refer to fmt.Sprintf() but most of the specifics will also apply to the other functions. The other Printf-like functions include fmt.Errorf()fmt.Fprintf()fmt.Fscanf()fmt.Printf()fmt.Scanf()fmt.Sscanf(), and log.Printf().

TL;DR: If you’re already familiar with Go’s Printf-like functionality, I recommend jumping to #4 below for help with argument-indexed verbs or #5 for help with common errors. Also, you can try my new tool fixit to automatically clean up fmt.Sprintf() statements in your code.

1. Use fmt.Sprintf()!

Even if fmt.Sprintf() is occasionally confusing, it is still worth using. It creates a clear code that neatly divides what is (relatively) constant from what is variable. That makes your code easier to read and maintain. Sprintf() also makes your code much cleaner by avoiding a bunch of string concatenations.

Here’s a simple example of Go code without fmt.Sprintf().

myString := "Results: " + results + " and more: " + more + "." 

We can achieve the same thing with cleaner code using fmt.Sprintf().

myString := fmt.Sprintf("Results: %s and more: %s.", results, more)

2. Don’t Cross the Streams

Don’t mix concatenation (i.e., using the +) and fmt.Sprintf(). It’s not a syntax error but looks bad. If you’re using fmt.Sprintf() there’s probably no need to also concatenate the format string together.

This is crossing the streams, which is bad, Egon!

fmt.Sprintf("Format %s " + name + "... Stuff %d", f, 5)

Instead, use all concatenations or, preferably, all fmt.Sprintf():

fmt.Sprintf("Format %s %s... Stuff %d", f, name, 5)

go development terraform devops golang

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated DevOps Developers

Hire our Dedicated DevOps Developers who have in-depth skills and expertise to develop an interactive and secure web application. Get custom DevOps solutions for your project.

Hire DevOps Developer

Looking to hire top DevOps developers at affordable prices? **[Hire DevOps Developer](https://hourlydeveloper.io/hire-dedicated-devops-developer/ "Hire DevOps Developer")** from **[HourlyDeveloper.io](https://hourlydeveloper.io/...

Golang Web Development:Th Best Programming Language in 2020

Golang Web Development is the new trend globally. Get all the details about the Golang development here. Hire Golang Developers.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company