本文整理匯總了Golang中fmt.State.Write方法的典型用法代碼示例。如果您正苦於以下問題:Golang State.Write方法的具體用法?Golang State.Write怎麽用?Golang State.Write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fmt.State
的用法示例。
在下文中一共展示了State.Write方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Format
// Format implements fmt.Formatter. It accepts format.State for
// language-specific rendering.
func (v Value) Format(s fmt.State, verb rune) {
var lang int
if state, ok := s.(format.State); ok {
lang, _ = language.CompactIndex(state.Language())
}
// Get the options. Use DefaultFormat if not present.
opt := v.format
if opt == nil {
opt = defaultFormat
}
cur := v.currency
if cur.index == 0 {
cur = opt.currency
}
// TODO: use pattern.
io.WriteString(s, opt.symbol(lang, cur))
if v.amount != nil {
s.Write(space)
// TODO: apply currency-specific rounding
scale, _ := opt.kind.Rounding(cur)
if _, ok := s.Precision(); !ok {
fmt.Fprintf(s, "%.*f", scale, v.amount)
} else {
fmt.Fprint(s, v.amount)
}
}
}
示例2: Format
func (af *ansiFormatter) Format(f fmt.State, c rune) {
// reconstruct the format string in bf
bf := new(bytes.Buffer)
bf.WriteByte('%')
for _, x := range []byte{'-', '+', '#', ' ', '0'} {
if f.Flag(int(x)) {
bf.WriteByte(x)
}
}
if w, ok := f.Width(); ok {
fmt.Fprint(bf, w)
}
if p, ok := f.Precision(); ok {
fmt.Fprintf(bf, ".%d", p)
}
bf.WriteRune(c)
format := bf.String()
if len(af.codes) == 0 {
fmt.Fprintf(f, format, af.value)
return
}
fmt.Fprintf(f, "\x1b[%d", af.codes[0])
for _, code := range af.codes[1:] {
fmt.Fprintf(f, ";%d", code)
}
f.Write([]byte{'m'})
fmt.Fprintf(f, format, af.value)
fmt.Fprint(f, "\x1b[0m")
}
示例3: Format
func (a Attributes) Format(fs fmt.State, c rune) {
for i, tv := range a {
fmt.Fprintf(fs, "%s %s", tv.Tag, tv.Value)
if i < len(a)-1 {
fs.Write([]byte("; "))
}
}
}
示例4: writeMultiple
// write count copies of text to s
func writeMultiple(s fmt.State, text string, count int) {
if len(text) > 0 {
b := []byte(text)
for ; count > 0; count-- {
s.Write(b)
}
}
}
示例5: Format
// We implement fmt.Formatter interface just so we can left-align the cards.
func (s Suit) Format(f fmt.State, c int) {
x := stringTable[s]
f.Write([]byte(x))
if w, ok := f.Width(); ok {
for i := 0; i < w-len(x); i++ {
f.Write([]byte{' '})
}
}
}
示例6: Format
// Format implements fmt.Formatter with support for the following verbs.
//
// %s source file
// %d line number
// %n function name
// %v equivalent to %s:%d
//
// It accepts the '+' and '#' flags for most of the verbs as follows.
//
// %+s path of source file relative to the compile time GOPATH
// %#s full path of source file
// %+n import path qualified function name
// %+v equivalent to %+s:%d
// %#v equivalent to %#s:%d
func (c Call) Format(s fmt.State, verb rune) {
if c.fn == nil {
fmt.Fprintf(s, "%%!%c(NOFUNC)", verb)
return
}
switch verb {
case 's', 'v':
file, line := c.fn.FileLine(c.pc)
switch {
case s.Flag('#'):
// done
case s.Flag('+'):
file = file[pkgIndex(file, c.fn.Name()):]
default:
const sep = "/"
if i := strings.LastIndex(file, sep); i != -1 {
file = file[i+len(sep):]
}
}
io.WriteString(s, file)
if verb == 'v' {
buf := [7]byte{':'}
s.Write(strconv.AppendInt(buf[:1], int64(line), 10))
}
case 'd':
_, line := c.fn.FileLine(c.pc)
buf := [6]byte{}
s.Write(strconv.AppendInt(buf[:0], int64(line), 10))
case 'n':
name := c.fn.Name()
if !s.Flag('+') {
const pathSep = "/"
if i := strings.LastIndex(name, pathSep); i != -1 {
name = name[i+len(pathSep):]
}
const pkgSep = "."
if i := strings.Index(name, pkgSep); i != -1 {
name = name[i+len(pkgSep):]
}
}
io.WriteString(s, name)
}
}
示例7: formatStruct
func (f *formatter) formatStruct(s fmt.State, c rune, val reflect.Value) {
f.depth++
if f.verbose {
writeType(s, val)
writeLeftcurly(s)
if f.pretty {
writeNewline(s)
writeFullIndent(s, f.depth)
}
} else {
writeLeftcurly(s)
}
typ := val.Type()
for i, n := 0, val.NumField(); i < n; i++ {
field := typ.Field(i)
if i > 0 {
f.sep(s)
}
if f.verbose || f.extra {
s.Write([]byte(field.Name))
writeColon(s)
}
if field.PkgPath == "" {
f.format(s, c, val.Field(i))
} else {
field := typ.Field(i)
var valptr reflect.Value
if val.CanAddr() {
valptr = val.Addr()
} else {
valptr = reflect.New(typ)
reflect.Indirect(valptr).Set(val)
}
fieldp := valptr.Pointer() + field.Offset
fieldptr := reflect.NewAt(field.Type, unsafe.Pointer(fieldp))
f.format(s, c, reflect.Indirect(fieldptr))
}
}
f.depth--
if f.verbose && f.pretty {
writeComma(s)
writeNewline(s)
writeFullIndent(s, f.depth)
}
writeRightcurly(s)
}
示例8: Format
// Format satisfies the fmt.Formatter interface. See NewFormatter for usage
// details.
func (f *formatState) Format(fs fmt.State, verb rune) {
f.fs = fs
// Use standard formatting for verbs that are not v.
if verb != 'v' {
format := f.constructOrigFormat(verb)
fmt.Fprintf(fs, format, f.value)
return
}
if f.value == nil {
if fs.Flag('#') {
fs.Write(interfaceBytes)
}
fs.Write(nilAngleBytes)
return
}
f.format(reflect.ValueOf(f.value))
}
示例9: Format
// Format allows text to satisfy the fmt.Formatter interface. The format
// behaviour is the same as for fmt.Print.
func (t text) Format(fs fmt.State, c rune) {
if t.Mode&activeBits != 0 {
t.Mode.set(fs)
}
w, wOk := fs.Width()
p, pOk := fs.Precision()
var (
b bytes.Buffer
prevString bool
)
b.WriteByte('%')
for _, f := range "+-# 0" {
if fs.Flag(int(f)) {
b.WriteRune(f)
}
}
if wOk {
fmt.Fprint(&b, w)
}
if pOk {
b.WriteByte('.')
fmt.Fprint(&b, p)
}
b.WriteRune(c)
format := b.String()
for _, v := range t.v {
isString := v != nil && doesString(v)
if isString && prevString {
fs.Write([]byte{' '})
}
prevString = isString
fmt.Fprintf(fs, format, v)
}
if t.Mode&activeBits != 0 && t.Mode&activeBits != Reset && t.Mode&NoResetAfter == 0 {
t.reset(fs)
}
}
示例10: format
func format(b Bed, fs fmt.State, c rune) {
bv := reflect.ValueOf(b)
if bv.IsNil() {
fmt.Fprint(fs, "<nil>")
return
}
bv = bv.Elem()
switch c {
case 'v':
if fs.Flag('#') {
fmt.Fprintf(fs, "&%#v", bv.Interface())
return
}
fallthrough
case 's':
width, _ := fs.Width()
if !b.canBed(width) {
fmt.Fprintf(fs, "%%!(BADWIDTH)%T", b)
return
}
if width == 0 {
width = bv.NumField()
}
for i := 0; i < width; i++ {
f := bv.Field(i).Interface()
if i >= rgbField {
switch i {
case rgbField:
rv := reflect.ValueOf(f)
if reflect.DeepEqual(rv.Interface(), color.RGBA{}) {
fs.Write([]byte{'0'})
} else {
fmt.Fprintf(fs, "%d,%d,%d",
rv.Field(0).Interface(), rv.Field(1).Interface(), rv.Field(2).Interface())
}
case blockCountField:
fmt.Fprint(fs, f)
case blockSizesField, blockStartsField:
av := reflect.ValueOf(f)
l := av.Len()
for j := 0; j < l; j++ {
fmt.Fprint(fs, av.Index(j).Interface())
if j < l-1 {
fs.Write([]byte{','})
}
}
}
} else {
fmt.Fprint(fs, f)
}
if i < width-1 {
fs.Write([]byte{'\t'})
}
}
default:
fmt.Fprintf(fs, "%%!%c(%T=%3s)", c, b, b)
}
}
示例11: Format
func (this *Person) Format(f fmt.State, c rune) {
if c == 'L' {
f.Write([]byte(this.String()))
f.Write([]byte(" Person has three fields."))
} else {
// 沒有此句,會導致 fmt.Printf("%s", p) 啥也不輸出
f.Write([]byte(fmt.Sprintln(this.String())))
}
}
示例12: Format
func (r Range) Format(f fmt.State, c int) {
i := byte(0)
for ; i < r.Min; i++ {
f.Write([]byte{'X'})
}
for ; float64(i)+0.5 < r.Mean; i++ {
f.Write([]byte{'x'})
}
for ; i < r.Max; i++ {
f.Write([]byte{'.'})
}
if w, ok := f.Width(); ok {
for ; i < byte(w); i++ {
f.Write([]byte{' '})
}
}
}
示例13: Format
// Format implements fmt.Formatter by printing the Trace as square brackes ([,
// ]) surrounding a space separated list of Calls each formatted with the
// supplied verb and options.
func (pcs Trace) Format(s fmt.State, c rune) {
s.Write([]byte("["))
for i, pc := range pcs {
if i > 0 {
s.Write([]byte(" "))
}
pc.Format(s, c)
}
s.Write([]byte("]"))
}
示例14: Format
// Format implements fmt.Formatter by printing the CallStack as square brackes
// ([, ]) surrounding a space separated list of Calls each formatted with the
// supplied verb and options.
func (cs CallStack) Format(s fmt.State, verb rune) {
s.Write([]byte("["))
for i, pc := range cs {
if i > 0 {
s.Write([]byte(" "))
}
pc.Format(s, verb)
}
s.Write([]byte("]"))
}
示例15: writeColon
func writeColon(s fmt.State) { s.Write(colonBytes) }