how print variable values in Go with fmt.Printl?

1.2k views Asked by At

I have the below code snippet:

x_variable := 12311
fmt.Println("message", "X variable has the value '+x_variable+' printed in the screen now")

How can I make that work? I've tried that in the Go playground but couldn't figure it out how to print the vaue properly.

2

There are 2 answers

0
Pratheesh M On BEST ANSWER
package main

import (
    "fmt"
)

func main() {
    x_variable := 12311
    fmt.Printf("message X variable has the value %d printed in the screen now", x_variable)
}
1
gpopides On

What you need is Sprintf

You can find already an example there.

Since you want to print an integer, you can use the %d modifier.

Keep in mind that Sprintf family does not insert newline automatically, so you will also need \n