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


Golang fmt.Stringer類代碼示例

本文整理匯總了Golang中fmt.Stringer的典型用法代碼示例。如果您正苦於以下問題:Golang Stringer類的具體用法?Golang Stringer怎麽用?Golang Stringer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Stringer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: String

func String(s fmt.Stringer) string {
	if reflect.ValueOf(s).IsNil() {
		return ""
	}

	return s.String()
}
開發者ID:coreos,項目名稱:coreos-metadata,代碼行數:7,代碼來源:metadata.go

示例2: TestGraphSupportsDownCasting

func TestGraphSupportsDownCasting(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		d fmt.Stringer
	)

	graph.Define(&d, inject.NewProvider(NewD))
	graph.ResolveAll()

	Expect(d).To(Equal(NewD()))
	Expect(d.String()).To(Equal("&ImplD{}"))

	expectedString := `&graph\{
  definitions: \[
    &definition\{
      ptr: \*fmt\.Stringer=0x.*,
      provider: &provider\{
        constructor: func\(\) \*test\.ImplD,
        argPtrs: \[\]
      \},
      value: <\*test\.ImplD Value>
    \}
  \]
\}`
	Expect(graph.String()).To(MatchRegexp(expectedString))
}
開發者ID:jdef,項目名稱:oinker-go,代碼行數:29,代碼來源:graph_test.go

示例3: TestInterfaces

func TestInterfaces(t *testing.T) {
	db, err := bolt.Open("my4.db", 0600, nil)
	if err != nil {
		t.Error(err.Error())
	}
	defer os.Remove("my4.db")
	defer db.Close()

	s := NewStore(db, []byte("interface"))

	var j fmt.Stringer = &MyType{"First", "Last"}
	s.Put([]byte("test"), &j)

	err = s.ForEach(func(str fmt.Stringer) {
		if str.String() != "First Last" {
			t.Errorf("unexpected string %s", str)
		}
	})
	if err != nil {
		t.Error(err.Error())
	}

	var i fmt.Stringer
	err = s.Get([]byte("test"), &i)
	if err != nil {
		t.Error(err.Error())
	} else {
		if i.String() != "First Last" {
			t.Errorf("unexpected string %s", i)
		}
	}
}
開發者ID:dz0ny,項目名稱:stow,代碼行數:32,代碼來源:stow_test.go

示例4: Insert

// Insert adds a new item to the hash map or, if the key already exists,
// replaces the current item with the new one.
func (pm *ParMap) Insert(item fmt.Stringer) {
	key := item.String()
	shard := pm.getShard(key)
	shard.mutex.Lock()
	shard.index[key] = item
	shard.mutex.Unlock()
}
開發者ID:pombredanne,項目名稱:pbtc,代碼行數:9,代碼來源:parmap.go

示例5: testStringMatch

func testStringMatch(t *testing.T, s fmt.Stringer, expected string) {
	actual := strings.TrimSpace(s.String())
	expected = strings.TrimSpace(expected)
	if actual != expected {
		t.Fatalf("Actual\n\n%s\n\nExpected:\n\n%s", actual, expected)
	}
}
開發者ID:malston,項目名稱:terraform-provider-bosh,代碼行數:7,代碼來源:terraform_test.go

示例6: assert

func assert(t *testing.T, o fmt.Stringer, s string) {
	if o.String() != s {
		t.Log("Expected object to be resolved differently as a String:")
		t.Logf("Object: %#v", o)
		t.Logf("Expected: %q", s)
		t.Fatalf("Got: %q", o.String())
	}
}
開發者ID:taskcluster,項目名稱:taskcluster-base-go,代碼行數:8,代碼來源:scopes_descriptions_test.go

示例7: AddWithFallback

func (v *ASTStringer) AddWithFallback(color string, what fmt.Stringer, fallback string) *ASTStringer {
	if !isNil(what) {
		v.AddStringColored(color, what.String())
	} else {
		v.AddStringColored(color, fallback)
	}
	return v
}
開發者ID:kiljacken,項目名稱:ark,代碼行數:8,代碼來源:ast.go

示例8: processLogMsg

func (cLogger *commonLogger) processLogMsg(level LogLevel, message fmt.Stringer, context LogContextInterface) {
	defer func() {
		if err := recover(); err != nil {
			reportInternalError(fmt.Errorf("recovered from panic during message processing: %s", err))
		}
	}()
	if cLogger.config.IsAllowed(level, context) {
		cLogger.config.RootDispatcher.Dispatch(message.String(), level, context, reportInternalError)
	}
}
開發者ID:ColBT,項目名稱:amazon-ecs-init,代碼行數:10,代碼來源:logger.go

示例9: Lookup

// Lookup finds the first node in the current scope by name.
func (s *Scope) Lookup(token fmt.Stringer) (ssa.Node, error) {
	name := token.String()
	if s.Empty() {
		return nil, fmt.Errorf("can't find a node '%s' in an empty scope stack", name)
	}
	for _, symtab := range s.stack.list {
		if symtab.contains(name) {
			return symtab.get(name)
		}
	}
	return nil, fmt.Errorf("'%s' not yet declared", name)
}
開發者ID:jackspirou,項目名稱:chip,代碼行數:13,代碼來源:scope.go

示例10: safeString

func safeString(str fmt.Stringer) (s string, ok bool) {
	defer func() {
		if panicVal := recover(); panicVal != nil {
			if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
				s, ok = "null", false
			} else {
				panic(panicVal)
			}
		}
	}()
	s, ok = str.String(), true
	return
}
開發者ID:qband,項目名稱:down,代碼行數:13,代碼來源:encode.go

示例11: safeString

func safeString(str fmt.Stringer) (s string) {
	defer func() {
		if panicVal := recover(); panicVal != nil {
			if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
				s = "NULL"
			} else {
				panic(panicVal)
			}
		}
	}()
	s = str.String()
	return
}
開發者ID:gledger,項目名稱:api,代碼行數:13,代碼來源:json_logger.go

示例12: Global

// Global sets a node and its name in the global scope.
func (s *Scope) Global(token fmt.Stringer, n ssa.Node) error {
	name := token.String()
	symtab, err := s.stack.bottom()
	if err != nil {
		return err
	}
	if symtab.contains(name) {
		return fmt.Errorf("'%s' already declared globally", name)
	}
	err = symtab.set(name, n)
	if err != nil {
		return err
	}
	return nil
}
開發者ID:jackspirou,項目名稱:chip,代碼行數:16,代碼來源:scope.go

示例13: processLogMsg

func (cLogger *commonLogger) processLogMsg(
	level LogLevel,
	message fmt.Stringer,
	context logContextInterface) {

	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()

	if cLogger.config.IsAllowed(level, context) {
		cLogger.config.RootDispatcher.Dispatch(message.String(), level, context, reportInternalError)
	}
}
開發者ID:stoiclabs,項目名稱:blockchainr,代碼行數:15,代碼來源:logger.go

示例14: Add

// Add adds a name to the topmost scope in the scope stack.
func (s *Scope) Add(token fmt.Stringer, n ssa.Node) error {
	name := token.String()
	if s.Contains(name) {
		return fmt.Errorf("'%s' already declared", name)
	}
	symtab, err := s.stack.peek()
	if err != nil {
		return err
	}
	err = symtab.set(name, n)
	if err != nil {
		return err
	}
	return nil
}
開發者ID:jackspirou,項目名稱:chip,代碼行數:16,代碼來源:scope.go

示例15: TestNXTStringer

func TestNXTStringer(t *testing.T) {
	name, devicePath := "testingbot", "/dev/tty-foobar"
	var n fmt.Stringer
	n = NewNXT(name, devicePath)

	if n == nil {
		t.Errorf("Calling NewNXT should not result in a nil NXT instance.")
		return
	}

	expected, actual := fmt.Sprintf("NXT \"%s\": %s", name, devicePath), n.String()
	if actual != expected {
		t.Errorf("Expected NXT.String to return \"%s\", but got \"%s\" instead", expected, actual)
	}
}
開發者ID:farshidtz,項目名稱:go-nxt,代碼行數:15,代碼來源:nxt_test.go


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