本文整理汇总了Golang中golang.org/x/tools/go/types/typeutil.Map.String方法的典型用法代码示例。如果您正苦于以下问题:Golang Map.String方法的具体用法?Golang Map.String怎么用?Golang Map.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/tools/go/types/typeutil.Map
的用法示例。
在下文中一共展示了Map.String方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestMap
func TestMap(t *testing.T) {
var tmap *typeutil.Map
// All methods but Set are safe on on (*T)(nil).
tmap.Len()
tmap.At(tPStr1)
tmap.Delete(tPStr1)
tmap.KeysString()
tmap.String()
tmap = new(typeutil.Map)
// Length of empty map.
if l := tmap.Len(); l != 0 {
t.Errorf("Len() on empty Map: got %d, want 0", l)
}
// At of missing key.
if v := tmap.At(tPStr1); v != nil {
t.Errorf("At() on empty Map: got %v, want nil", v)
}
// Deletion of missing key.
if tmap.Delete(tPStr1) {
t.Errorf("Delete() on empty Map: got true, want false")
}
// Set of new key.
if prev := tmap.Set(tPStr1, "*string"); prev != nil {
t.Errorf("Set() on empty Map returned non-nil previous value %s", prev)
}
// Now: {*string: "*string"}
// Length of non-empty map.
if l := tmap.Len(); l != 1 {
t.Errorf("Len(): got %d, want 1", l)
}
// At via insertion key.
if v := tmap.At(tPStr1); v != "*string" {
t.Errorf("At(): got %q, want \"*string\"", v)
}
// At via equal key.
if v := tmap.At(tPStr2); v != "*string" {
t.Errorf("At(): got %q, want \"*string\"", v)
}
// Iteration over sole entry.
tmap.Iterate(func(key types.Type, value interface{}) {
if key != tPStr1 {
t.Errorf("Iterate: key: got %s, want %s", key, tPStr1)
}
if want := "*string"; value != want {
t.Errorf("Iterate: value: got %s, want %s", value, want)
}
})
// Setion with key equal to present one.
if prev := tmap.Set(tPStr2, "*string again"); prev != "*string" {
t.Errorf("Set() previous value: got %s, want \"*string\"", prev)
}
// Setion of another association.
if prev := tmap.Set(tChanInt1, "<-chan int"); prev != nil {
t.Errorf("Set() previous value: got %s, want nil", prev)
}
// Now: {*string: "*string again", <-chan int: "<-chan int"}
want1 := "{*string: \"*string again\", <-chan int: \"<-chan int\"}"
want2 := "{<-chan int: \"<-chan int\", *string: \"*string again\"}"
if s := tmap.String(); s != want1 && s != want2 {
t.Errorf("String(): got %s, want %s", s, want1)
}
want1 = "{*string, <-chan int}"
want2 = "{<-chan int, *string}"
if s := tmap.KeysString(); s != want1 && s != want2 {
t.Errorf("KeysString(): got %s, want %s", s, want1)
}
// Keys().
I := types.Identical
switch k := tmap.Keys(); {
case I(k[0], tChanInt1) && I(k[1], tPStr1): // ok
case I(k[1], tChanInt1) && I(k[0], tPStr1): // ok
default:
t.Errorf("Keys(): got %v, want %s", k, want2)
}
if l := tmap.Len(); l != 2 {
t.Errorf("Len(): got %d, want 1", l)
}
// At via original key.
if v := tmap.At(tPStr1); v != "*string again" {
t.Errorf("At(): got %q, want \"*string again\"", v)
}
hamming := 1
tmap.Iterate(func(key types.Type, value interface{}) {
switch {
case I(key, tChanInt1):
hamming *= 2 // ok
case I(key, tPStr1):
hamming *= 3 // ok
//.........这里部分代码省略.........