Golang fmt.Fprintf() function examples

**package fmt***

Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered.

Golang fmt.Fprintf() function usage examples

Example 1:

 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()) //<--- here
 return
  case x == nil:
 fmt.Fprint(s, "<nil>")
 return
  }
 ...

Example 2:

 ...
 var helptext = `Usage: gosteno-prettify [OPTS] [FILE(s)]
 Parses json formatted log lines from FILE(s), or stdin,
 and displays a more human friendly version of each line to stdout.
 Examples :

 gosteno-prettify f - g

 Prettify f's contents, then standard input, then g's contents.
 gosteno-prettify
 Prettify contents of stdin.
 Options:
 -h
 Display help
 -a
 Omit location and data in order to provide well-aligned logs
 -s
 Do not complain about errors in parsing logs
 `

 flag.Usage = func() {
 fmt.Fprintf(os.Stderr, helptext)
 }
 ...

Reference :

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

Advertisement