当前位置: 首页>>代码示例>>Golang>>正文


Golang MapValue.Keys方法代码示例

本文整理汇总了Golang中reflect.MapValue.Keys方法的典型用法代码示例。如果您正苦于以下问题:Golang MapValue.Keys方法的具体用法?Golang MapValue.Keys怎么用?Golang MapValue.Keys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reflect.MapValue的用法示例。


在下文中一共展示了MapValue.Keys方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: writeMap

func writeMap(w io.Writer, val *reflect.MapValue) (err os.Error) {
	key := val.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return &MarshalError{val.Type()}
	}

	keys := val.Keys()
	if _, err = fmt.Fprint(w, "{"); err != nil {
		return
	}

	for i := 0; i < len(keys); i++ {
		if _, err = fmt.Fprintf(w, "%s:", Quote(keys[i].(*reflect.StringValue).Get())); err != nil {
			return
		}

		if err = writeValue(w, val.Elem(keys[i])); err != nil {
			return
		}

		if i < len(keys)-1 {
			if _, err = fmt.Fprint(w, ","); err != nil {
				return
			}
		}
	}

	_, err = fmt.Fprint(w, "}")
	return
}
开发者ID:ivanwyc,项目名称:google-go,代码行数:30,代码来源:struct.go

示例2: writeMap

func writeMap(w io.Writer, val *reflect.MapValue) (err os.Error) {
	key := val.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return &MarshalError{val.Type()}
	}
	_, err = fmt.Fprint(w, "d")
	if err != nil {
		return
	}

	keys := val.Keys()

	// Sort keys

	svList := make(StringValueArray, len(keys))
	for i, key := range keys {
		svList[i].key = key.(*reflect.StringValue).Get()
		svList[i].value = val.Elem(key)
	}

	err = writeSVList(w, svList)
	if err != nil {
		return
	}

	_, err = fmt.Fprint(w, "e")
	if err != nil {
		return
	}
	return
}
开发者ID:quantumelixir,项目名称:Taipei-Torrent,代码行数:31,代码来源:struct.go

示例3: PackMap

func PackMap(writer io.Writer, value *reflect.MapValue) (n int, err os.Error) {
	keys := value.Keys()
	if value.Len() < 16 {
		n, err := writer.Write([]byte{0x80 | byte(len(keys))})
		if err != nil {
			return n, err
		}
		for _, k := range keys {
			_n, err := PackValue(writer, k)
			if err != nil {
				return n, err
			}
			n += _n
			_n, err = PackValue(writer, value.Elem(k))
			if err != nil {
				return n, err
			}
			n += _n
		}
	} else if value.Len() < 65536 {
		n, err := writer.Write([]byte{0xde, byte(len(keys) >> 8), byte(len(keys))})
		if err != nil {
			return n, err
		}
		for _, k := range keys {
			_n, err := PackValue(writer, k)
			if err != nil {
				return n, err
			}
			n += _n
			_n, err = PackValue(writer, value.Elem(k))
			if err != nil {
				return n, err
			}
			n += _n
		}
	} else {
		n, err := writer.Write([]byte{0xdf, byte(len(keys) >> 24), byte(len(keys) >> 16), byte(len(keys) >> 8), byte(len(keys))})
		if err != nil {
			return n, err
		}
		for _, k := range keys {
			_n, err := PackValue(writer, k)
			if err != nil {
				return n, err
			}
			n += _n
			_n, err = PackValue(writer, value.Elem(k))
			if err != nil {
				return n, err
			}
			n += _n
		}
	}
	return n, nil
}
开发者ID:mattn,项目名称:msgpack,代码行数:56,代码来源:pack.go

示例4: encodeMap

// encodeMap encodes a map as unsigned count followed by key:value pairs.
// Because map internals are not exposed, we must use reflection rather than
// addresses.
func (enc *Encoder) encodeMap(b *bytes.Buffer, mv *reflect.MapValue, keyOp, elemOp encOp, keyIndir, elemIndir int) {
	state := enc.newEncoderState(b)
	state.fieldnum = -1
	state.sendZero = true
	keys := mv.Keys()
	state.encodeUint(uint64(len(keys)))
	for _, key := range keys {
		encodeReflectValue(state, key, keyOp, keyIndir)
		encodeReflectValue(state, mv.Elem(key), elemOp, elemIndir)
	}
	enc.freeEncoderState(state)
}
开发者ID:richlowe,项目名称:gcc,代码行数:15,代码来源:encode.go

示例5: writeMap

func (e *encodeState) writeMap(v *reflect.MapValue) {
	if v.IsNil() {
		return
	}
	if _, ok := v.Type().(*reflect.MapType).Key().(*reflect.StringType); !ok {
		e.abort(&EncodeTypeError{v.Type()})
	}
	offset := e.beginDoc()
	for _, k := range v.Keys() {
		e.encodeValue(k.(*reflect.StringValue).Get(), v.Elem(k))
	}
	e.WriteByte(0)
	e.endDoc(offset)
}
开发者ID:hokapoka,项目名称:go-mongo,代码行数:14,代码来源:bson_encode.go

示例6: writeMap

func writeMap(w io.Writer, val *reflect.MapValue) os.Error {
  writeByte(w, SMALL_TUPLE);
  writeByte(w, 3);
  writeAtom(w, "bert");
  writeAtom(w, "dict");
  
  keys := val.Keys();
  writeByte(w, LIST);
  binary.Write(w, binary.BigEndian, uint32(len(keys)));
  for i := 0; i < len(keys); i++ {
    w.Write([]byte {SMALL_TUPLE, 2});
    writeValue(w, keys[i]);
    writeValue(w, val.Elem(keys[i]));
  }
  writeByte(w, NIL);

  return nil;
}
开发者ID:nicholassm,项目名称:gobert,代码行数:18,代码来源:bert.go

示例7: writeMap

func writeMap(w io.Writer, val *reflect.MapValue) os.Error {
	key := val.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return &MarshalError{val.Type()}
	}

	keys := val.Keys()
	fmt.Fprint(w, "{")
	for i := 0; i < len(keys); i++ {
		fmt.Fprintf(w, "%q:", keys[i].(*reflect.StringValue).Get())

		if err := writeValue(w, val.Elem(keys[i])); err != nil {
			return err
		}

		if i < len(keys)-1 {
			fmt.Fprint(w, ",")
		}
	}

	fmt.Fprint(w, "}")
	return nil
}
开发者ID:rapgamer,项目名称:golang-china,代码行数:23,代码来源:struct.go

示例8: fuzzyEqualMap

func fuzzyEqualMap(a *reflect.MapValue, b *reflect.MapValue) bool {
	key := a.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return false
	}
	key = b.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return false
	}

	aKeys, bKeys := a.Keys(), b.Keys()

	if len(aKeys) != len(bKeys) {
		return false
	}

	for _, k := range aKeys {
		if !fuzzyEqualValue(a.Elem(k), b.Elem(k)) {
			return false
		}
	}
	return true
}
开发者ID:royger,项目名称:wgo,代码行数:23,代码来源:bencode_test.go


注:本文中的reflect.MapValue.Keys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。