I can't speak about track racing, since I never did that, but in road races--take 5K as an example--I found a couple of things to be useful:
1) Race rehearsal. I'd jog the entire course as a warmup and talk to myself as I arrived at particular moments--one mile in, halfway, one mile to go, final quarter-mile, particular steep hills, etc. I'd describe how I was going to be feeling at that moment in the race. The point was to think it all through, FEEL it all through, in advance, so that when I actually arrived at that moment in the race, everything was going according to plan rather than assaulting me in a disillusioning way. That way, when I got to the final half mile or so and I was hurting like hell, I could say to myself, "Here we go. Nothing surprising about THIS."
2) I always resolved that I was going to hurt on my own terms, not on anybody else's terms. So I tried hard not to go out too fast; I held myself back. Bob Glover's phrase "The first half is for pacing, the second half is for racing" was my mantra. Any race in which pain forced me to slow between the one-mile point and the halfway point--i.e., shortly after passing the first mile marker--I considered a pacing failure. That meant that I would carry pain with me throughout the race, rather than deliberately bringing it on in the final mile. The latter sort of race is the race I wanted to run.