It happens, so learn to deal with it. In the city its' unavoidable, and takes nerves of steel. I avoid city riding for the most part. In the countryside, get a good mirror, check trajectory of every vehicle coming up behind you with one quick glance at the critical distance (maybe 50'), and bail onto shoulder or even into ditch if there is one, if you think they're going to hit you. If it just looks like a close pass, learn to hold firm on your line and avoid panic reaction. Swerving either way is potentially dangerous. Holding your line is a skill you will develop, and it serves you well when startled by sudden loud noise like gunshot or whatnot. Holding your line isn't so easy if a dog charges you from the side and isn't slowing down as it's about to hit your front wheel. Still working on that one....