我们看到 Format 里面具体处理逻辑在
AppendFormat 函数里面,再点开 AppendFormat
看看:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
switch std & stdMask { case stdYear: y := year if y < 0 { y = -y } b = appendInt(b, y%100, 2) case stdLongYear: b = appendInt(b, year, 4) case stdMonth: b = append(b, month.String()[:3]...) case stdLongMonth: m := month.String() b = append(b, m...) // ... 省略其他 case }
我们可以看到里面的 stdYear、stdLongYear
之类的常量实际上就是我们传入到 Format
的参数里面的数字。
stdNeedDate = 1 << 8// need month, day, year stdNeedClock = 2 << 8// need hour, minute, second stdArgShift = 16// extra argument in high bits, above low stdArgShift stdSeparatorShift = 28// extra argument in high 4 bits for fractional second separators stdMask = 1<<stdArgShift - 1// mask out argument )