String formatting in Go without console output: how to get a formatted string?
Hello everyone! I just started learning Go and encountered a problem. In Python, I often used the `.format()` method for strings to assemble a string with variables, but not output it immediately, but, for example, save it to a variable for further use (logging, sending over network, writing to file).
In Go, I see there is a `fmt.Printf()` function that perfectly formats and outputs text to `stdout`. But what if I just need to get a formatted string without printing it to the screen? Let's say I want to build an SQL query from a template and values:
```go
name := "Alice"
age := 30
// I want to get the string: "User: Alice, Age: 30"
// fmt.Printf("User: %s, Age: %d", name, age) // outputs to console, but I need to save it to a variable
```
I tried using `fmt.Sprintf()`, but it doesn't
No answers yet.
Log in to leave an answer
Blogs