In Go, string formatting can be done using the fmt
package. The fmt
package provides various functions and verbs that allow you to format strings.
To format a string, you can use the Printf
function from the fmt
package. It takes a format string as the first argument, followed by other arguments that need to be formatted.
The format string contains placeholder verbs that represent the type of data to be formatted. Some commonly used verbs are:
- %s - formats a string
- %d - formats an integer
- %f - formats a floating-point number
- %t - formats a boolean value
- %v - formats any value in a default format
- %T - formats the type of a value
Here's a simple example that demonstrates string formatting:
1 2 3 4 5 6 7 8 9 10 11 12 |
package main import "fmt" func main() { name := "John" age := 30 height := 5.11 // Formatting strings using verbs fmt.Printf("Name: %s, Age: %d, Height: %.2f\n", name, age, height) } |
Output:
1
|
Name: John, Age: 30, Height: 5.11
|
In the example above, we use the %s
verb to format the name
variable as a string, %d
to format the age
variable as an integer, and %.2f
to format the height
variable as a float with 2 decimal places.
You can combine multiple verbs in the format string to format multiple values at once.
These are the basic concepts of string formatting in Go using the fmt
package. There are additional formatting options available, and you can refer to the Go documentation for more details.
What is the purpose of html.EscapeString function in Golang?
The purpose of the html.EscapeString
function in Golang is to sanitize and escape a given string, making it safe to be embedded in an HTML document.
This function replaces special characters (<
, >
, &
, '
, "
) with their corresponding HTML entity codes (<
, >
, &
, '
, "
).
By using html.EscapeString
, it helps prevent Cross-Site Scripting (XSS) attacks, as any user input or untrusted data can be safely displayed in HTML without being interpreted as code or potentially harmful content.
What is the purpose of %x format verb in Golang?
In Golang, the %x
format verb is used to format a given value as a hexadecimal string. It is used in conjunction with the fmt.Printf
or fmt.Sprintf
functions to produce formatted output.
The %x
verb specifies that the value should be converted to its hexadecimal representation. It works with various types including integers, byte slices, and strings.
Here's an example of using %x
verb with fmt.Printf
:
1 2 |
number := 42 fmt.Printf("Hexadecimal representation: %x\n", number) |
Output:
1
|
Hexadecimal representation: 2a
|
In this example, the decimal value 42
is formatted as a hexadecimal string 2a
using the %x
verb.
What is the purpose of net/url package for URL encoding in Golang?
The purpose of the net/url
package in Golang is to provide functionality for working with URLs. It allows you to parse, manipulate, and encode URLs.
Specifically, the package includes functions and types for URL encoding and decoding. The net/url
package provides the URL
type, which represents a parsed URL. It has methods to access various components of the URL, such as the scheme, host, path, query parameters, and fragment.
URL encoding is important when working with URLs, as it ensures that special characters and reserved characters are properly encoded. The net/url
package provides the QueryEscape
function to encode URL query parameters.
Overall, the net/url
package is essential for working with URLs in a robust and secure manner, handling encoding and decoding operations effectively.
How to format the current date and time as a string in Golang?
To format the current date and time as a string in Golang, you can use the time
package and its Format()
function. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package main import ( "fmt" "time" ) func main() { currentTime := time.Now() // Format the date and time string using a specific format layout dateTimeString := currentTime.Format("2006-01-02 15:04:05") fmt.Println(dateTimeString) } |
In the example above, time.Now()
returns the current date and time in Go's time.Time
format. The Format()
function is then called on this value to convert it to a string representation. The layout string "2006-01-02 15:04:05" specifies how the date and time should be formatted: 2006
represents the year, 01
represents the month, 02
represents the day, 15
represents the hour in 24-hour format, 04
represents the minute, and 05
represents the second.
When you run this code, it will output the current date and time in the specified format, such as "2022-07-18 13:45:10".