當前位置: 首頁>>代碼示例>>Golang>>正文


Golang State.Write方法代碼示例

本文整理匯總了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)
		}
	}
}
開發者ID:ChongFeng,項目名稱:beats,代碼行數:32,代碼來源:format.go

示例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")
}
開發者ID:ak-67,項目名稱:vuvuzela,代碼行數:31,代碼來源:ansi.go

示例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("; "))
		}
	}
}
開發者ID:gordon,項目名稱:biogo,代碼行數:8,代碼來源:gff.go

示例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)
		}
	}
}
開發者ID:Sunmonds,項目名稱:gcc,代碼行數:9,代碼來源:int.go

示例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{' '})
		}
	}
}
開發者ID:droundy,項目名稱:abridge,代碼行數:10,代碼來源:suit.go

示例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)
	}
}
開發者ID:Xetius,項目名稱:grafana,代碼行數:60,代碼來源:stack.go

示例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)
}
開發者ID:bmatsuo,項目名稱:go-dfmt,代碼行數:46,代碼來源:dfmt.go

示例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))
}
開發者ID:2722,項目名稱:lantern,代碼行數:22,代碼來源:format.go

示例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)
	}
}
開發者ID:0-T-0,項目名稱:ct,代碼行數:42,代碼來源:ct.go

示例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)
	}
}
開發者ID:gordon,項目名稱:biogo,代碼行數:58,代碼來源:bed.go

示例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())))
	}
}
開發者ID:cwen-coder,項目名稱:study-gopkg,代碼行數:9,代碼來源:Formatter.go

示例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{' '})
		}
	}
}
開發者ID:droundy,項目名稱:abridge,代碼行數:17,代碼來源:ensemble.go

示例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("]"))
}
開發者ID:ContiGuy,項目名稱:conti-gui,代碼行數:13,代碼來源:stack.go

示例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("]"))
}
開發者ID:gunjan5,項目名稱:Skywalker,代碼行數:13,代碼來源:stack.go

示例15: writeColon

func writeColon(s fmt.State)                 { s.Write(colonBytes) }
開發者ID:bmatsuo,項目名稱:go-dfmt,代碼行數:1,代碼來源:dfmt.go


注:本文中的fmt.State.Write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。