Skip to content

Instantly share code, notes, and snippets.

@troyk
Created May 29, 2014 17:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troyk/896c045094ec93c1578b to your computer and use it in GitHub Desktop.
Save troyk/896c045094ec93c1578b to your computer and use it in GitHub Desktop.
idea to avoid using pointers in go structs just for happy patching
type Repository struct {
Name string `json:"name"`
Description string `json:"description"`
Private bool `json:"private"`
}
// response to blog post https://willnorris.com/2014/05/go-rest-apis-and-pointers
type RepositoryPatch struct {
Repository
patched_fields []string
}
func (rp *RepositoryPatch) SetPrivate(private bool) {
rp.patch_fields = append(rp.patch_fields,"private")
rp.Private = private
}
func (rp *RepositoryPatch) MarshalJSON() ([]byte, error) {
// marshal rp to []byte then unmarshal to map[string]interface{}
// filter patched_fields and then marshal again
// ;)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment