Pointing to true - currently initializing pointer to true

I have a JSON struct of the following form:

type postJSON struct {
	ButtonA *bool `json:"button_a,omitempty"`
	ButtonB *bool `json:"button_b,omitempty"`

This allows me to omit false from a sent message, i.e. only true values are sent.

I currently have to setup a (package) variable like this:

var pressed = new(bool)
	post_json := postJSON{}
	if strings.Contains(msg, "a") {
		post_json.ButtonA = pressed

func init() {
	*pressed = true

Are there better ways to setup pressed - or to do this? I’m really looking for something like:

post_json.ButtonA = &true

N.B. I do not want to create (make) a new true pointer to boolean every time - so making it and assigning it would not be (I think) a good solution.

Thank you - Andy

I would remove all the *bool complications and instead make the fields bool and add custom marshaling methods for postJSON that understand that false is to be omitted. That will make the struct much easier to work with.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.