Get LayerMask that current GameObject collides with?

Basically I have a MonoBehaviour script which I make a sphere cast with. Currently I’ve set it up so that I have a public LayerMask that I set manually. But I just manually set it to the layers that the game object currently collides with! It’d be so much nicer if there was an easy way to get the current collision mask for the current game object through code. How can I do this?

This code works well:

 public static LayerMask GetCollisionMaskOf(GameObject go)
        {
            int myLayer = go.layer;
            int layerMask = 0;
            for (int i = 0; i < 32; i++)
            {
                if (!Physics.GetIgnoreLayerCollision(myLayer, i))
                {
                    layerMask = layerMask | 1 << i;
                }
            }
            return layerMask;
        }