Golang fmt.Fprint() function examples

package fmt

Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

Golang fmt.Fprint() function usage examples

Example 1:

 func (h Hello) ServeHTTP( w http.ResponseWriter,  r *http.Request) {
 fmt.Fprint(w, "Hello!")
 }

Example 2:

 func (x *Int) Format(s fmt.State, ch rune) {
  cs := charset(ch)

  // special cases
  switch {
  case cs == "":
 // unknown format
 fmt.Fprintf(s, "%%!%c(big.Int=%s)", ch, x.String())
 return
  case x == nil:
 fmt.Fprint(s, "<nil>")
 return
  }
 ...
 }

Example 3 :

 package main

 import (
 "crypto/sha256"
 "fmt"
 "encoding/hex"
 )

 func hash(c string) string {
 h := sha256.New()
 fmt.Fprint(h, c)
 return hex.EncodeToString(h.Sum(nil))
 }


 func main() {
 fmt.Println(hash("abcd"))
 }

Reference :

http://golang.org/pkg/fmt/#Fprint

Advertisement