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
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
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
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.
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
myString := "Results: " + results + " and more: " + more + "."
We can achieve the same thing with cleaner code using
myString := fmt.Sprintf("Results: %s and more: %s.", results, more)
Don’t mix concatenation (i.e., using the
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("Format %s %s... Stuff %d", f, name, 5)
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.
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 is the new trend globally. Get all the details about the Golang development here. Hire Golang Developers.
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.
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