Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
String Formatting | Data Types
Introduction to GoLang

String FormattingString Formatting

Now that we have learned about all the basic data types, we can explore string formatting. In programming, string formatting involves incorporating variable values into strings.

Note

The import statement is used to import packages into a Go application. A package is a collection of related Go source files that collaborate to offer a set of functionalities or services. The fmt package provides functions that allow us to format strings.

One of the functions from the fmt package that we have been using is the Println function. The Println function displays output text on a new line each time. However, there is a function called Print that displays output without moving to a new line:

go

index.go

In general, we can use these functions to insert variable values within string data using the following method:

go

index.go

However, this method is not very efficient because we have to divide the string into many segments to insert variables at relevant places, which can become cumbersome in more complex cases. The Printf function addresses this issue and simplifies the process of displaying formatted output.

The Printf function takes a format string as its first argument, followed by values (or variables) to be inserted into the format string. A format string contains placeholders (format specifiers) that define the desired output's format.

Following is an example:

go

index.go

As illustrated by the example, Format Specifiers are symbols that serve as placeholders for specific data types within a string. There is a distinct format specifier for each data type. The table below provides a list of basic format specifiers:

Format SpecifierData Type
%dInteger
%sString
%fFloat
%tBoolean
%cCharacter (or Rune)

The variables substitute the respective placeholders in the format string. In the output of the example above, the first %d is replaced by the value of n. Similarly, the second is substituted with the corresponding variable, which is in, and the last %d is replaced by the value of out.

We can also format a string and store it inside a string variable using the Sprintf function, which formats a string and returns the resulting string for storage or use elsewhere, rather than immediately displaying it on the screen:

go

index.go

Here's a more complex example that incorporates multiple concepts we've previously covered in a single program. It will be a valuable exercise to review the following program and its output to enhance your code-reading skills:

go

index.go

In some cases, the compiler performs implicit type casting to match values with format specifiers, as demonstrated in the following example:

go

index.go

The rune 'char' is outputted both as a character and as a numerical value. However, in some cases, the compiler might produce an error. For example, the following code will result in an invalid output because a 'float' cannot be implicitly converted into an 'int':

go

index.go

What will be the output of the program below?

Selecione a resposta correta

Tudo estava claro?

Seção 2. Capítulo 7
course content

Conteúdo do Curso

Introduction to GoLang

String FormattingString Formatting

Now that we have learned about all the basic data types, we can explore string formatting. In programming, string formatting involves incorporating variable values into strings.

Note

The import statement is used to import packages into a Go application. A package is a collection of related Go source files that collaborate to offer a set of functionalities or services. The fmt package provides functions that allow us to format strings.

One of the functions from the fmt package that we have been using is the Println function. The Println function displays output text on a new line each time. However, there is a function called Print that displays output without moving to a new line:

go

index.go

In general, we can use these functions to insert variable values within string data using the following method:

go

index.go

However, this method is not very efficient because we have to divide the string into many segments to insert variables at relevant places, which can become cumbersome in more complex cases. The Printf function addresses this issue and simplifies the process of displaying formatted output.

The Printf function takes a format string as its first argument, followed by values (or variables) to be inserted into the format string. A format string contains placeholders (format specifiers) that define the desired output's format.

Following is an example:

go

index.go

As illustrated by the example, Format Specifiers are symbols that serve as placeholders for specific data types within a string. There is a distinct format specifier for each data type. The table below provides a list of basic format specifiers:

Format SpecifierData Type
%dInteger
%sString
%fFloat
%tBoolean
%cCharacter (or Rune)

The variables substitute the respective placeholders in the format string. In the output of the example above, the first %d is replaced by the value of n. Similarly, the second is substituted with the corresponding variable, which is in, and the last %d is replaced by the value of out.

We can also format a string and store it inside a string variable using the Sprintf function, which formats a string and returns the resulting string for storage or use elsewhere, rather than immediately displaying it on the screen:

go

index.go

Here's a more complex example that incorporates multiple concepts we've previously covered in a single program. It will be a valuable exercise to review the following program and its output to enhance your code-reading skills:

go

index.go

In some cases, the compiler performs implicit type casting to match values with format specifiers, as demonstrated in the following example:

go

index.go

The rune 'char' is outputted both as a character and as a numerical value. However, in some cases, the compiler might produce an error. For example, the following code will result in an invalid output because a 'float' cannot be implicitly converted into an 'int':

go

index.go

What will be the output of the program below?

Selecione a resposta correta

Tudo estava claro?

Seção 2. Capítulo 7
some-alt