예제 #1
0
파일: hash.go 프로젝트: herokai/circuit
func snapvalue(w io.Writer, v reflect.Value) {
	var q string
	switch v.Kind() {

	case reflect.Bool: // Not addressable
		q = strconv.FormatBool(v.Bool())

	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		q = strconv.FormatInt(v.Int(), 36)

	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		q = strconv.FormatUint(v.Uint(), 36)

	case reflect.Uintptr:
		panic("n/s")

	case reflect.Float32, reflect.Float64:
		q = strconv.FormatFloat(v.Float(), 'g', 65, 64)

	case reflect.Complex64, reflect.Complex128:
		c := v.Complex()
		q = "(" + strconv.FormatFloat(real(c), 'g', 65, 64) + ", " + strconv.FormatFloat(imag(c), 'g', 65, 64) + "i)"

	case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer: // Addressable
		q = strconv.FormatUint(uint64(v.Pointer()), 36) // uintptr

	case reflect.Interface:
		d := v.InterfaceData() // [2]uintptr
		q = "<" + strconv.FormatUint(uint64(d[0]), 36) + "," + strconv.FormatUint(uint64(d[1]), 36) + ">"

	case reflect.String:
		q = v.String()

	case reflect.Array:
		w.Write([]byte("{"))
		for i := 0; i < v.Len(); i++ {
			snapvalue(w, v.Index(i))
			w.Write([]byte(","))
		}
		w.Write([]byte("}"))
		return

	case reflect.Struct:
		w.Write([]byte("{"))
		for i := 0; i < v.NumField(); i++ {
			snapvalue(w, v.FieldByIndex([]int{i}))
			w.Write([]byte(","))
		}
		w.Write([]byte("}"))

	default:
		panic("u")
	}
	w.Write([]byte(q))
}