If I wanted to do the following:
a := "%shello%s"
b:= fmt.Sprintf("%sWorld",a)
fmt.Printf(b)
I want to print
%shello%sWorld
ie %s is replaced only in %sWorld.
How can i do that?
I do not want to replace a with %%shello%%s
If I wanted to do the following:
a := "%shello%s"
b:= fmt.Sprintf("%sWorld",a)
fmt.Printf(b)
I want to print
%shello%sWorld
ie %s is replaced only in %sWorld.
How can i do that?
I do not want to replace a with %%shello%%s
a := "%shello%s"
b:= fmt.Sprintf("%sWorld",a)
This works just fine, it results in a string being "%shello%sWorld"
.
The problem is with how you print it:
fmt.Printf(b)
fmt.Printf()
treats b
as a format string, and since b
's value contains %s
, this expects you to also pass arguments (which you haven't), so the actual output contains error messages.
Instead print it with fmt.Println()
:
fmt.Println(b)
And output will be (try it on the Go Playground):
%shello%sWorld