Golang fmt with example
In Golang,
fmt
is a standard library package that provides formatted I/O functionality. It can be used to print output to the console, format strings, and read input from the console. Here's an example of how to use fmt
in Golang:package main
import "fmt"
func main() {
// Print to console
fmt.Println("Hello, world!")
// Format string
age := 30
name := "John"
fmt.Printf("%s is %d years old\n", name, age)
// Read input from console
var input string
fmt.Print("Enter a string: ")
fmt.Scanln(&input)
fmt.Printf("You entered: %s\n", input)
}
In the above example, we import the
fmt
package and use the Println
function to print "Hello, world!" to the console. We then use the Printf
function to format a string with the name
and age
variables. Finally, we use Scanln
to read input from the console and Printf
to print the input back to the console.
Comments
Post a Comment