Skip to content

Instantly share code, notes, and snippets.

@Braden-Preston
Created October 24, 2023 03:15
Show Gist options
  • Save Braden-Preston/ed60f27a8ceecf67ce78d2f90c35206e to your computer and use it in GitHub Desktop.
Save Braden-Preston/ed60f27a8ceecf67ce78d2f90c35206e to your computer and use it in GitHub Desktop.
Custom Go Type with Marshallers
package form
import (
"encoding/json"
"gopkg.in/guregu/null.v4/zero"
)
type OptionalText string
func (s *OptionalText) MarshalJSON() ([]byte, error) {
var z zero.String
z.String = string(*s)
return json.Marshal(z.String)
}
func (s *OptionalText) UnmarshalJSON(data []byte) error {
text := string(data)
*s = OptionalText(text)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment