Skip to content

Commit 74a54bd

Browse files
committed
cmd: use omitzero option for time.Time
The doc of time.Time says: Programs using times should typically store and pass them as values, not pointers. That is, time variables and struct fields should be of type time.Time, not *time.Time. Since CL 615676 added omitzero option to encoding/json package, we can just replace (*time.Time + omitempty) with (time.Time + omitzero). Change-Id: I5593c4b45af291d015433bd1d164f1c1378db642
1 parent b38415d commit 74a54bd

File tree

5 files changed

+10
-11
lines changed

5 files changed

+10
-11
lines changed

src/cmd/go/internal/modinfo/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type ModulePublic struct {
1919
Query string `json:",omitempty"` // version query corresponding to this version
2020
Versions []string `json:",omitempty"` // available module versions
2121
Replace *ModulePublic `json:",omitempty"` // replaced by this module
22-
Time *time.Time `json:",omitempty"` // time version was created
22+
Time time.Time `json:",omitzero"` // time version was created
2323
Update *ModulePublic `json:",omitempty"` // available update (with -u)
2424
Main bool `json:",omitempty"` // is this the main module?
2525
Indirect bool `json:",omitempty"` // module is only indirectly needed by main module

src/cmd/go/internal/modload/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func addUpdate(ctx context.Context, m *modinfo.ModulePublic) {
156156
m.Update = &modinfo.ModulePublic{
157157
Path: m.Path,
158158
Version: info.Version,
159-
Time: &info.Time,
159+
Time: info.Time,
160160
}
161161
}
162162
}
@@ -344,7 +344,7 @@ func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode Li
344344
m.Error = &modinfo.ModuleError{Err: err.Error()}
345345
} else {
346346
m.Version = q.Version
347-
m.Time = &q.Time
347+
m.Time = q.Time
348348
}
349349
}
350350

src/cmd/go/internal/modload/query.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ func queryProxy(ctx context.Context, proxy, path, query, current string, allowed
234234
Version: old.Version,
235235
Origin: old.Origin,
236236
}
237-
if old.Time != nil {
238-
info.Time = *old.Time
237+
if !old.Time.IsZero() {
238+
info.Time = old.Time
239239
}
240240
return info, nil
241241
}

src/cmd/go/internal/work/action.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ type actionJSON struct {
175175
NeedBuild bool `json:",omitempty"`
176176
ActionID string `json:",omitempty"`
177177
BuildID string `json:",omitempty"`
178-
TimeReady time.Time `json:",omitempty"`
179-
TimeStart time.Time `json:",omitempty"`
180-
TimeDone time.Time `json:",omitempty"`
178+
TimeReady time.Time `json:",omitzero"`
179+
TimeStart time.Time `json:",omitzero"`
180+
TimeDone time.Time `json:",omitzero"`
181181

182182
Cmd []string // `json:",omitempty"`
183183
CmdReal time.Duration `json:",omitempty"`

src/cmd/internal/test2json/test2json.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929

3030
// event is the JSON struct we emit.
3131
type event struct {
32-
Time *time.Time `json:",omitempty"`
32+
Time time.Time `json:",omitzero"`
3333
Action string
3434
Package string `json:",omitempty"`
3535
Test string `json:",omitempty"`
@@ -402,8 +402,7 @@ func (c *Converter) writeOutputEvent(out []byte) {
402402
func (c *Converter) writeEvent(e *event) {
403403
e.Package = c.pkg
404404
if c.mode&Timestamp != 0 {
405-
t := time.Now()
406-
e.Time = &t
405+
e.Time = time.Now()
407406
}
408407
if e.Test == "" {
409408
e.Test = c.testName

0 commit comments

Comments
 (0)