fmt.Sprintln returns a String, but doesn't print anything. (The name was taken from the also confusingly named C function sprintf.)
What you need is Printf, but you have to add the newline yourself:
fmt.Printf("inc 1 equal %v\n", inc(1))
Answer from Thomas on Stack OverflowDoes anyone know of an easier way to interpolate strings?
This works:
cheese := "gouda"
fmt.Printf("I like %s", cheese)
But it would so much nicer if there is a way to put the variables in the string like many other languages. For a single variable, it's not so bad, but when you have a bunch, it isn't very efficient.
Eg. Python:chesse = "gouda"print(f"I like {cheese}")
or Dart:String cheese = "gouda"print("I like $cheese");
How to interpolate multiline string?
proposal: Go 2: string interpolation
Named String Formatting
String interpolation with range
Videos
fmt.Sprintln returns a String, but doesn't print anything. (The name was taken from the also confusingly named C function sprintf.)
What you need is Printf, but you have to add the newline yourself:
fmt.Printf("inc 1 equal %v\n", inc(1))
Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.
Sprint format a string and returns such a string, it does write nothing. What you're searching for is Print
Furthermore, the variant ln doesn't parse %, it only add the new line character at the end of the string.
So, if you want to write to standard output using format, you should use this:
fmt.Printf("inc 1 equal %v", inc(1))