gostring-interpolation

Go string interpolation


Trying to interpolate an int value into a string using %v formatter as follows, yet nothing is printed,

package main

import "fmt"

func inc(i int) int {
  return i + 1
}

func main() {
  fmt.Sprintln("inc 1 equal %v", inc(1))
}

How to interpolate an int value ?


Solution

  • 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))