Basic fmt

suggest change

Package fmt implements formatted I/O using format verbs:

Verb Meaning
%v default format
%T type of the value
%s uninterpreted bytes of the string or slice

Formatting functions

There are 4 main function types in fmt and several variations within.

Print, Println, Printf

fmt.Print("Hello World")        // prints: Hello World
fmt.Println("Hello World")      // prints: Hello World\n
fmt.Printf("Hello %s", "World") // prints: Hello World


formattedString := fmt.Sprintf("%v %s", 2, "words") // returns string "2 words"


byteCount, err := fmt.Fprint(w, "Hello World") // writes to io.Writer w

Fprint can be used inside http handlers:

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello %s!", "Browser")
}   // Writes: "Hello Browser!" to HTTP response


Scan scans text read from standard input.

var s string
fmt.Scanln(&s) // pass pointer to buffer
// Scanln is similar to fmt.Scan(), but it stops scanning at new line.
fmt.Println(s) // whatever was inputted

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents