9.6 C
New York
Thursday, March 23, 2023

How to Format Strings in Go – MUO – MakeUseOf

Find out how to use string formatting verbs and which Go functions support them.
As you write Go code, you’ll find string formatting very useful in many situations. You might be parsing inputs or crafting more complex output than simple concatenation. You might be working with types other than plain strings.
String formatting in Go uses the familiar process and syntax of the printf function, which languages from Java to Haskell also use.
Go provides various methods for formatting strings in the fmt package. You can use functions and verbs for string formatting depending on the operation or inputs you want to format.
The functions in the fmt package are similar to counterparts, like the printf function in bash or C. Go derives its formatting verbs from C.
You use string formatting verbs as placeholders for your variable values in a containing string. You can then pass that formatting string to a function like Printf, along with values corresponding to those placeholders.
You can’t use string formatting verbs with the Print and Println methods. You can use them with methods like Printf and Sprintf.
The %v verb prints any value in its default format. The Println method doesn’t recognize verbs and prints whatever arguments it receives. The Printf and Sprintf functions both format the first string argument you pass to them.
Formatting strings in the Go programming language requires you to use a string formatting function and a verb. The function returns the formatted string, and the verbs are the placeholders for the inputs to the string.
The Printf method formats the input according to the format specifier and returns the number of written bytes or errors.
Conventionally, you won’t have to worry about errors when you use the Printf method.
The Sprintf method formats according to the specified format and returns the result as a string.
The Fprintf method formats the string and writes it to a writer (methods that implement the io.Writer interface)
The Fscanf method scans from a reader and formats according to the specified format.
In this case, the Fscanf decodes the string from the reader into the take variable, and the read variable holds the result of the format.
Go provides many formatting verbs that you can use along with the string formatting functions.
There are general string formatting verbs like the %v verb in the string formatting functions examples. You can use the general string formatting verbs to format any data type.
You can use the %#v verb to output any value, the %+v for structs, the %T verb for the type of any value, and the %% verb for no values.
The result variable holds the formatted string of the instantiated struct. If you print it out, it should look something like this:
There are the verbs for formatting specific Go native data types, including channels and pointers.
You’ll want to ensure that you don’t make mistakes with the verbs since they are case-sensitive, like the chan and pointer verbs.
There are string formatting verbs for formatting integers in different bases. You can use any of these verbs to format integers
For example, you can format an integer using the %d verb:
These are the verbs for formatting floating point numbers.
Here’s an example of formatting a decimal point without exponent with the %f verb.
You can always use the general verbs if you’re uncertain about the type.
Strings and slice of byte types are pretty similar in Go. These are the flags for formatting strings and bytes.
Here’s an example of formatting a string with the %s verb.
The fmt package contains most of the functionality you’ll need for string formatting. Go also provides a strings package for string manipulation and a log package that can format strings for logging.
The fmt package has functions that implement the io.Writer and io.Reader interfaces. You’ll find it useful for many use cases like building web and command line applications.
Goodness is a mechanical engineering student and software developer passionate about cloud technologies and the Go programming language.
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!

source

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles