So the object can have either open or close. What's the best way to handle it in go?
Assistant
User
Do I need omitempty if I'm just decoding, not encoding?
Assistant
Good question!
No, you do not need omitempty if you are only decoding (unmarshalling) JSON into Go structs.
omitempty is a struct tag option that only affects encoding (marshalling):
When encoding (Go -> JSON): omitempty tells the encoder to omit that field if it has a zero value (like nil pointers, 0 for numbers, empty slices, etc.).
When decoding (JSON -> Go): the omitempty tag has no effect; Go will decode whatever is present in the JSON into the struct fields.
So, for your use case (just decoding/unmarshalling):
You can safely omit omitempty:
go
type TimeEntry struct {
Open *int `json:"open"`
Close *int `json:"close"`
}
and it will work exactly the same during unmarshalling.
Why do people often include it?
You have an API that returns a structure like this:
i.e., each day is a list of objects that either have an "open" or a "close" field.
How to handle this in Go?
You want to parse this JSON into a Go struct that represents the open and close times. Because each item has only one of the two possible keys, you need a struct where both keys are pointers or use omitempty so you can distinguish the values presence.
Recommended Approach
Define a struct with two fields: Open and Close, both pointers to int (or int64, depending on your data). When unmarshalling, only one of the fields will be set.
go
type TimeEntry struct {
Open *int `json:"open,omitempty"`
Close *int `json:"close,omitempty"`
}
type DaySchedule []TimeEntry
type WeekSchedule struct {
Monday DaySchedule `json:"monday"`
Tuesday DaySchedule `json:"tuesday"`
Wednesday DaySchedule `json:"wednesday"`
Thursday DaySchedule `json:"thursday"`
Friday DaySchedule `json:"friday"`
Saturday DaySchedule `json:"saturday"`
Sunday DaySchedule `json:"sunday"`
}
Example usage
go
package main
import (
"encoding/json"
"fmt"
)
type TimeEntry struct {
Open *int `json:"open,omitempty"`
Close *int `json:"close,omitempty"`
}
type DaySchedule []TimeEntry
type WeekSchedule struct {
Monday DaySchedule `json:"monday"`
// add other days as needed...
}
func main() {
data := []byte(`
{
"monday": [
{ "open": 28800 },
{ "close": 72000 }
]
}`)
var schedule WeekSchedule
err := json.Unmarshal(data, &schedule)
if err != nil {
panic(err)
}
for _, entry := range schedule.Monday {
if entry.Open != nil {
fmt.Printf("Open time: %d\n", *entry.Open)
}
if entry.Close != nil {
fmt.Printf("Close time: %d\n", *entry.Close)
}
}
}
Output:
code
Open time: 28800
Close time: 72000
Notes
Using pointers to int lets you differentiate between "open": 0 and "open" absent.
You could also use a single struct with a "marker" field or unmarshal into map[string]int then convert to your struct, but the above is clean and straightforward.
This approach supports multiple open/close intervals per day (if your API returns multiple).
If you want, I can help you further with features such as validating the sequence of open/close times or creating more domain-specific types. Let me know!
It's often included by habit or for coding consistency, especially if the structs are also used to encode JSON later.
Summary
For unmarshalling: omitempty can be omitted and has no effect.
For marshalling: omitempty ensures zero-value fields are skipped.
Feel free to remove the omitempty tag if you don't plan on marshalling!