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


Golang Value.Slice方法代码示例

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


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

示例1: reflectSliceWithProperType

// reflectSliceWithProperType does the opposite thing as setSliceWithProperType.
func reflectSliceWithProperType(key *Key, field reflect.Value, delim string) error {
	slice := field.Slice(0, field.Len())
	if field.Len() == 0 {
		return nil
	}

	var buf bytes.Buffer
	sliceOf := field.Type().Elem().Kind()
	for i := 0; i < field.Len(); i++ {
		switch sliceOf {
		case reflect.String:
			buf.WriteString(slice.Index(i).String())
		case reflect.Int, reflect.Int64:
			buf.WriteString(fmt.Sprint(slice.Index(i).Int()))
		case reflect.Uint, reflect.Uint64:
			buf.WriteString(fmt.Sprint(slice.Index(i).Uint()))
		case reflect.Float64:
			buf.WriteString(fmt.Sprint(slice.Index(i).Float()))
		case reflectTime:
			buf.WriteString(slice.Index(i).Interface().(time.Time).Format(time.RFC3339))
		default:
			return fmt.Errorf("unsupported type '[]%s'", sliceOf)
		}
		buf.WriteString(delim)
	}
	key.SetValue(buf.String()[:buf.Len()-1])
	return nil
}
开发者ID:stellar,项目名称:bridge-server,代码行数:29,代码来源:struct.go

示例2: decComplex128Array

func decComplex128Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
	// Can only slice if it is addressable.
	if !v.CanAddr() {
		return false
	}
	return decComplex128Slice(state, v.Slice(0, v.Len()), length, ovfl)
}
开发者ID:pjump,项目名称:gcc,代码行数:7,代码来源:dec_helpers.go

示例3: marshalSimple

func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []byte, error) {
	switch val.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return strconv.FormatInt(val.Int(), 10), nil, nil
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return strconv.FormatUint(val.Uint(), 10), nil, nil
	case reflect.Float32, reflect.Float64:
		return strconv.FormatFloat(val.Float(), 'g', -1, val.Type().Bits()), nil, nil
	case reflect.String:
		return val.String(), nil, nil
	case reflect.Bool:
		return strconv.FormatBool(val.Bool()), nil, nil
	case reflect.Array:
		if typ.Elem().Kind() != reflect.Uint8 {
			break
		}
		// [...]byte
		var bytes []byte
		if val.CanAddr() {
			bytes = val.Slice(0, val.Len()).Bytes()
		} else {
			bytes = make([]byte, val.Len())
			reflect.Copy(reflect.ValueOf(bytes), val)
		}
		return "", bytes, nil
	case reflect.Slice:
		if typ.Elem().Kind() != reflect.Uint8 {
			break
		}
		// []byte
		return "", val.Bytes(), nil
	}
	return "", nil, &UnsupportedTypeError{typ}
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:34,代码来源:marshal.go

示例4: Unpack

func (f *Field) Unpack(buf []byte, val reflect.Value, length int, options *Options) error {
	typ := f.Type.Resolve(options)
	if typ == Pad || f.kind == reflect.String {
		if typ == Pad {
			return nil
		} else {
			val.SetString(string(buf))
			return nil
		}
	} else if f.Slice {
		if val.Cap() < length {
			val.Set(reflect.MakeSlice(val.Type(), length, length))
		} else if val.Len() < length {
			val.Set(val.Slice(0, length))
		}
		// special case byte slices for performance
		if !f.Array && typ == Uint8 && f.defType == Uint8 {
			copy(val.Bytes(), buf[:length])
			return nil
		}
		pos := 0
		size := typ.Size()
		for i := 0; i < length; i++ {
			if err := f.unpackVal(buf[pos:pos+size], val.Index(i), 1, options); err != nil {
				return err
			}
			pos += size
		}
		return nil
	} else {
		return f.unpackVal(buf, val, length, options)
	}
}
开发者ID:arunk-s,项目名称:libaudit-go,代码行数:33,代码来源:field.go

示例5: grow

// grow grows the slice s so that it can hold extra more values, allocating
// more capacity if needed. It also returns the old and new slice lengths.
func grow(s reflect.Value, extra int) (reflect.Value, int, int) {
	i0 := s.Len()
	i1 := i0 + extra
	if i1 < i0 {
		panic("reflect.Append: slice overflow")
	}
	m := s.Cap()
	if i1 <= m {
		return s.Slice(0, i1), i0, i1
	}
	if m == 0 {
		m = extra
	} else {
		for m < i1 {
			if i0 < 1024 {
				m += m
			} else {
				m += m / 4
			}
		}
	}
	t := reflect.MakeSlice(s.Type(), i1, m)
	reflect.Copy(t, s)
	return t, i0, i1
}
开发者ID:alicebob,项目名称:json,代码行数:27,代码来源:stdlib_slice.go

示例6: sequence

func (d *decoder) sequence(n *node, out reflect.Value) (good bool) {
	l := len(n.children)

	var iface reflect.Value
	switch out.Kind() {
	case reflect.Slice:
		out.Set(reflect.MakeSlice(out.Type(), l, l))
	case reflect.Interface:
		// No type hints. Will have to use a generic sequence.
		iface = out
		out = settableValueOf(make([]interface{}, l))
	default:
		d.terror(n, yaml_SEQ_TAG, out)
		return false
	}
	et := out.Type().Elem()

	j := 0
	for i := 0; i < l; i++ {
		e := reflect.New(et).Elem()
		if ok := d.unmarshal(n.children[i], e); ok {
			out.Index(j).Set(e)
			j++
		}
	}
	out.Set(out.Slice(0, j))
	if iface.IsValid() {
		iface.Set(out)
	}
	return true
}
开发者ID:danprince,项目名称:yaml,代码行数:31,代码来源:decode.go

示例7: encodeByteArray

func encodeByteArray(b []byte, v reflect.Value) []byte {
	n := v.Len()

	if n < (0xec - 0xe0) {
		b = append(b, byte(0xe0+n))
	} else {
		b = encodeK4(b, 0xec, uint64(n))
	}

	// Fast path for when the array is addressable (which it almost
	// always will be).
	if v.CanAddr() {
		return append(b, v.Slice(0, n).Bytes()...)
	}

	i := len(b)
	j := i + n

	if j > cap(b) {
		t := make([]byte, i, j)
		copy(t, b)
		b = t
	}

	reflect.Copy(reflect.ValueOf(b[i:j]), v)
	return b[:j]
}
开发者ID:erkl,项目名称:binn,代码行数:27,代码来源:encoder.go

示例8: Slice

func Slice(expectedSlice reflect.Value, actualSlice reflect.Value) (bool, diff.Difference) {
	for i := 0; i < actualSlice.Len(); i++ {
		if i >= expectedSlice.Len() {
			return false, diff.SliceExtraElements{
				ExtraElements: actualSlice.Slice(i, actualSlice.Len()),
				AllElements:   actualSlice,
			}
		}

		equal, difference := Compare(expectedSlice.Index(i).Interface(), actualSlice.Index(i).Interface())
		if !equal {
			return false, diff.SliceNested{
				Index:            i,
				NestedDifference: difference,
			}
		}
	}

	if expectedSlice.Len() > actualSlice.Len() {
		return false, diff.SliceMissingElements{
			MissingElements: expectedSlice.Slice(actualSlice.Len(), expectedSlice.Len()),
			AllElements:     actualSlice,
		}
	}

	return true, diff.NoDifference{}
}
开发者ID:pivotal-cf-experimental,项目名称:gomegamatchers,代码行数:27,代码来源:slice.go

示例9: reflectWithProperType

// reflectWithProperType does the opposite thing with setWithProperType.
func reflectWithProperType(t reflect.Type, key *Key, field reflect.Value, delim string) error {
	switch t.Kind() {
	case reflect.String:
		key.SetValue(field.String())
	case reflect.Bool,
		reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
		reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
		reflect.Float64,
		reflectTime:
		key.SetValue(fmt.Sprint(field))
	case reflect.Slice:
		vals := field.Slice(0, field.Len())
		if field.Len() == 0 {
			return nil
		}

		var buf bytes.Buffer
		isTime := fmt.Sprint(field.Type()) == "[]time.Time"
		for i := 0; i < field.Len(); i++ {
			if isTime {
				buf.WriteString(vals.Index(i).Interface().(time.Time).Format(time.RFC3339))
			} else {
				buf.WriteString(fmt.Sprint(vals.Index(i)))
			}
			buf.WriteString(delim)
		}
		key.SetValue(buf.String()[:buf.Len()-1])
	default:
		return fmt.Errorf("unsupported type '%s'", t)
	}
	return nil
}
开发者ID:CadeLaRen,项目名称:docker-3,代码行数:33,代码来源:struct.go

示例10: decodeSliceValue

func decodeSliceValue(d *Decoder, v reflect.Value) error {
	n, err := d.DecodeArrayLen()
	if err != nil {
		return err
	}

	if n == -1 {
		v.Set(reflect.Zero(v.Type()))
		return nil
	}
	if n == 0 && v.IsNil() {
		v.Set(reflect.MakeSlice(v.Type(), 0, 0))
		return nil
	}

	if v.Cap() >= n {
		v.Set(v.Slice(0, n))
	} else if v.Len() < v.Cap() {
		v.Set(v.Slice(0, v.Cap()))
	}

	for i := 0; i < n; i++ {
		if i >= v.Len() {
			v.Set(growSliceValue(v, n))
		}
		sv := v.Index(i)
		if err := d.DecodeValue(sv); err != nil {
			return err
		}
	}

	return nil
}
开发者ID:h12w,项目名称:msgpack,代码行数:33,代码来源:decode_slice.go

示例11: encComplex64Array

func encComplex64Array(state *encoderState, v reflect.Value) bool {
	// Can only slice if it is addressable.
	if !v.CanAddr() {
		return false
	}
	return encComplex64Slice(state, v.Slice(0, v.Len()))
}
开发者ID:2thetop,项目名称:go,代码行数:7,代码来源:enc_helpers.go

示例12: encode

func (encoder *Encoder) encode(v reflect.Value) error {

	switch v.Kind() {
	case reflect.Map:
		return encoder.encodeMap(v)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return encoder.encodeUint(v.Uint())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return encoder.encodeInt(v.Int())
	case reflect.String:
		return encoder.encodeString(v.String())
	case reflect.Array:
		v = v.Slice(0, v.Len())
		return encoder.encodeSlice(v)
	case reflect.Slice:
		return encoder.encodeSlice(v)
	case reflect.Float64, reflect.Float32:
		return encoder.encodeFloat(v.Float())
	case reflect.Interface:
		v = reflect.ValueOf(v.Interface())
		return encoder.encode(v)
	case reflect.Ptr:
		if v.IsNil() {
			return encoder.encodeNull()
		}
		vv := reflect.Indirect(v)
		if vv.Kind() == reflect.Struct {
			return encoder.encodeStruct(v)
		}
		return encoder.encode(vv)
	}

	return errors.New("unsupported type:" + v.Type().String())
}
开发者ID:edgarcai,项目名称:goamf-1,代码行数:34,代码来源:encoder.go

示例13: encodeFixedArray

// encodeFixedArray writes the XDR encoded representation of each element
// in the passed array represented by the reflection value to the encapsulated
// writer and returns the number of bytes written.  The ignoreOpaque flag
// controls whether or not uint8 (byte) elements should be encoded individually
// or as a fixed sequence of opaque data.
//
// A MarshalError is returned if any issues are encountered while encoding
// the array elements.
//
// Reference:
// 	RFC Section 4.12 - Fixed-Length Array
// 	Individually XDR encoded array elements
func (enc *Encoder) encodeFixedArray(v reflect.Value, ignoreOpaque bool) (int, error) {
	// Treat [#]byte (byte is alias for uint8) as opaque data unless ignored.
	if !ignoreOpaque && v.Type().Elem().Kind() == reflect.Uint8 {
		// Create a slice of the underlying array for better efficiency
		// when possible.  Can't create a slice of an unaddressable
		// value.
		if v.CanAddr() {
			return enc.EncodeFixedOpaque(v.Slice(0, v.Len()).Bytes())
		}

		// When the underlying array isn't addressable fall back to
		// copying the array into a new slice.  This is rather ugly, but
		// the inability to create a constant slice from an
		// unaddressable array is a limitation of Go.
		slice := make([]byte, v.Len(), v.Len())
		reflect.Copy(reflect.ValueOf(slice), v)
		return enc.EncodeFixedOpaque(slice)
	}

	// Encode each array element.
	var n int
	for i := 0; i < v.Len(); i++ {
		n2, err := enc.encode(v.Index(i))
		n += n2
		if err != nil {
			return n, err
		}
	}

	return n, nil
}
开发者ID:FihlaTV,项目名称:bridge-server,代码行数:43,代码来源:encode.go

示例14: DeleteSliceElementVal

func DeleteSliceElementVal(sliceVal reflect.Value, idx int) reflect.Value {
	if idx < 0 || idx >= sliceVal.Len() {
		return sliceVal
	}
	before := sliceVal.Slice(0, idx)
	after := sliceVal.Slice(idx+1, sliceVal.Len())
	sliceVal = reflect.AppendSlice(before, after)
	return sliceVal
}
开发者ID:zwczou,项目名称:goserver,代码行数:9,代码来源:slices.go

示例15: reflectAppend

func reflectAppend(i int, iface interface{}, slicev reflect.Value) reflect.Value {
	iv := reflect.ValueOf(iface)
	if slicev.Len() == i {
		slicev = reflect.Append(slicev, iv)
		slicev = slicev.Slice(0, slicev.Cap())
	} else {
		slicev.Index(i).Set(iv)
	}
	return slicev
}
开发者ID:gunosy,项目名称:dynamo,代码行数:10,代码来源:decode.go


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