In the UK, you are allowed to use reasonable force. If you do have a gun and you shoot an assailant with it, you would probably be ok. The law recognises that, in the heat of the moment, you might not have time to weigh up the options and choose a less lethal weapon.
On the other hand, if the assailant sees your gun and thinks better of attacking you and starts to run away, it would not be considered reasonable force to shoot them in the back. You will be in a lot of trouble.
Note that it is highly unlikely that the assailant would be in possession of a firearm themselves.