Golang fmt.State type examples

package fmt

State represents the printer state passed to custom formatters. It provides access to the io.Writer interface plus information about the flags and options for the operand's format specifier.

Golang fmt.State type usage examples

Example 1:

 var s fmt.State
 sign := ""
 switch {
 case s.Flag('+'): 
 sign = "+"
 case s.Flag(' '):
 sign = " "
 }

Example 2:

 func writeMany(text string, st fmt.State, counter int) {
  if len(text) > 0 {
 b := []byte(text)
 for ; counter > 0; counter-- {
 st.Write(b)
 }
  }
 }

Example 3:

 var f fmt.State
 var s string

 if w, ok := f.Width(); ok {
 s += fmt.Sprintf("%d", w)
 }

Reference :

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

Advertisement