Use the time.Time fields in archive/tar.Header and specify FormatPAX to preserve sub-second precision or FormatGNU for full compatibility. The default FormatUnknown rounds ModTime to the nearest second and ignores AccessTime and ChangeTime.
hdr := &tar.Header{
Name: "file.txt",
Size: 1024,
ModTime: time.Now(),
AccessTime: time.Now(),
ChangeTime: time.Now(),
Format: tar.FormatPAX,
}
err := tw.WriteHeader(hdr)