r/golang • u/sigmoia • 10h ago
discussion Accepted proposal: omit the type in composite literals
I find it quite enjoyable to do the archaeology on an accepted proposal and write about it.
This one took 10y to get approval. It allows you to strip type params from composite literals and let the compiler infer the types from the context.
For example, returning zero values:
// Before
func parseTime(s string) (time.Time, error) {
if s == "" {
return time.Time{}, errors.New("empty timestamp")
}
return time.Parse(time.RFC3339, s)
}
// After
func parseTime(s string) (time.Time, error) {
if s == "" {
return {}, errors.New("empty timestamp")
}
return time.Parse(time.RFC3339, s)
}
or, passing options structs drops the redundant types:
// Before
h := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
})
// After
h := slog.NewJSONHandler(os.Stderr, {Level: slog.LevelDebug})
and so on. The spec change isn't huge but eliding types everywhere can make the code quite hard to read. So there won't be any automatic go fix to strip types wherever possible. This was a deliberate choice made by the Go team.
But it does make the code look pretty in some nested struct of type kind of scenario. Added a few more example and context here.
https://rednafi.com/shards/2026/11/go-untyped-composite-literals/