Having a gun along is nice, and so is having an extra 20 lbs of water is case the springs run dry, and maybe a portable generator and TV and satellite antenna--but remember that this is the backpackingLIGHT forum and talk of heavyweight gear is not appropriate here, except to be disparaged. So unless your gun is very lightweight (please specify the weight, incidentally, including holster and bullets, and also indicate whether you performed any modifications such as drilling holes in the gun's handle), I don't think this is the right place for your question.
In any event, the main thing is to consider what could actually go wrong and your options. To defend against sticks and knives (or machetes), all you need is a stick of your own to ward off the initial attack, and then a good pair of legs to run away if the other guy is much bigger than you. Because you're reading backpackinglight, you will no doubt be wearing trail runners and so should be able to outrun somone wearing heavy boots. I see this as the most likely situation. Someone gets enraged because you bumped his gear in a shelter and goes for your throat. Parry the attack, then run away if the other guy is much bigger. Of course, he may destroy your precious gear that you left behind in revenge, but then look on the bright side--your packweight will now be zero!
For handguns at close range, whoever wins the draw wins the fight. So even if you are packing a gun, it won't help unless you can whip it out before the other guy gets his out. Self-defense instructors warn again and again of how difficult it can be to get to a gun when you really need it.
For handguns at a distance, just run. Very few people can hit a moving target with a handgun, especially in field conditions. Make sure to zig-zag and crouch down to make things more difficult. And by distance, I'm talking about 20 feet.
For defense against long guns and bows, a handgun might be of use. For example, suppose someone's coon dog attacks you and you fight back with a hiking stick. The owner gets *BEEP* off and points his gun at you. You might be able to jump behind a tree and pull out your handgun and thereby defend yourself, whereas without a gun of your own, you'd be dead meat, at least if the other guy is any kind of marksman.
My own experience is mostly in Europe, where I've been attacked numerous times by dogs. The worst incident involved a huge pit bull (at least 100 pounds) that charged from behind some bushes and then literally lept for my throat. I had only seconds to react, but this was sufficient to turn, point my trusty hiking stick at the dogs gaping mouth, and brace myself for the impact. The stick caught the dog on the front teeth or jaw. I heard some sort of crack, and it wasn't my stick (as I verified later) so I think I might have broken one of the dog's teeth. In any case, he trotted off immediately afterwards, wobbling a bit and shaking his head as if dizzy, then barked a few times (in a high-pitched and meek-sounding voice that was quite different from the throaty growl that had alerted me to his attack) when I was already some ways away. The owners of the dog were sitting on the porch of their farmhouse about 50 yards off, drinking beers and laughing the whole time. My guide book had warned about this location. Future backpackers, at least those wielding sticks, will perhaps have less trouble from this particular animal than me. (BTW I'm not a dog hater by any means, any more than I hate humans just because I run into vicious humans now and then.)
I wouldn't think of backpacking in either Europe or North America without a sturdy hiking stick as protection. But a handgun? I think that's overkill (no pun intended).