Any方法会根据value的类型返回不同的Field,如果value没有实现zapcore.ObjectMarshaler、zapcore.ArrayMarshaler,也不是基础类型,则走的是默认的Reflect(key, val)
Reflectzap@v1.16.0/field.go
func Reflect(key string, val interface{}) Field { return Field{Key: key, Type: zapcore.ReflectType, Interface: val}}Reflect创建的Field类型的Type为zapcore.ReflectType
AddTozap@v1.16.0/zapcore/field.go
func (f Field) AddTo(enc ObjectEncoder) { var err error switch f.Type { case ArrayMarshalerType: err = enc.AddArray(f.Key, f.Interface.(ArrayMarshaler)) case ObjectMarshalerType: err = enc.AddObject(f.Key, f.Interface.(ObjectMarshaler)) case BinaryType: enc.AddBinary(f.Key, f.Interface.([]byte)) case BoolType: enc.AddBool(f.Key, f.Integer == 1) case ByteStringType: enc.AddByteString(f.Key, f.Interface.([]byte)) case Complex128Type: enc.AddComplex128(f.Key, f.Interface.(complex128)) case Complex64Type: enc.AddComplex64(f.Key, f.Interface.(complex64)) case DurationType: enc.AddDuration(f.Key, time.Duration(f.Integer)) case Float64Type: enc.AddFloat64(f.Key, math.Float64frombits(uint64(f.Integer))) case Float32Type: enc.AddFloat32(f.Key, math.Float32frombits(uint32(f.Integer))) case Int64Type: enc.AddInt64(f.Key, f.Integer) case Int32Type: enc.AddInt32(f.Key, int32(f.Integer)) case Int16Type: enc.AddInt16(f.Key, int16(f.Integer)) case Int8Type: enc.AddInt8(f.Key, int8(f.Integer)) case StringType: enc.AddString(f.Key, f.String) case TimeType: if f.Interface != nil { enc.AddTime(f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location))) } else { // Fall back to UTC if location is nil. enc.AddTime(f.Key, time.Unix(0, f.Integer)) } case TimeFullType: enc.AddTime(f.Key, f.Interface.(time.Time)) case Uint64Type: enc.AddUint64(f.Key, uint64(f.Integer)) case Uint32Type: enc.AddUint32(f.Key, uint32(f.Integer)) case Uint16Type: enc.AddUint16(f.Key, uint16(f.Integer)) case Uint8Type: enc.AddUint8(f.Key, uint8(f.Integer)) case UintptrType: enc.AddUintptr(f.Key, uintptr(f.Integer)) case ReflectType: err = enc.AddReflected(f.Key, f.Interface) case NamespaceType: enc.OpenNamespace(f.Key) case StringerType: err = encodeStringer(f.Key, f.Interface, enc) case ErrorType: encodeError(f.Key, f.Interface.(error), enc) case SkipType: break default: panic(fmt.Sprintf("unknown field type: %v", f)) } if err != nil { enc.AddString(fmt.Sprintf("%sError", f.Key), err.Error()) }}
推荐阅读
-
-
-
聪聪食光|进入11月,迎桃花好彩头,喜结良缘喜脱单,这4生肖苦盼姻缘到来
-
纳税记录|您关注的个税纳税记录查询、积分落户等问题,权威解答来了
-
-
【兵哥有话说】俄罗斯趁机神速发展,西伯利亚力量-2进展乐观!,美国疫情危机
-
中国网娱乐|《奔跑吧》收视第一再攀新高 跑男马戏团让快乐循环
-
通科技|皖通科技中报业绩预计骤降6成 董事长“走马灯”更迭
-
小妹侃篮球|25+15+5!周琦又虐菜,但这数据辣眼:3场三分17中3
-
吃货|老外看到中国人在“啃棍子”,好奇也拿来啃,老外:竟然这么好吃
-
-
-
看看新闻Knews综合|威尔·史密斯妻子公开承认出轨 对象是儿子朋友
-
-
-
-
-
“我出钱出力帮你带孩子,换不来一点尊重吗”“是你们应该做的”
-
园中房产|16万亿楼市挤海绵!自住房子不足50%?马云任正非说实情
-
主动脉|经纪人曝黄鸿升死因:主动脉剥离造成血管阻塞