The rule is really very simple. It depends on the SOUND at the start of the following word. (It does not depend on the way we WRITE the following word, it depends on the way we SAY it.)
1. a + consonant SOUND : If the following word starts with a consonant SOUND, then we say a:
Example : a cat / a game of golf / a human emotion
2. an + vowel SOUND : If the following word starts with a vowel SOUND, then we say an:
Example : an apple / an easy job / an old man
3. The Importance of SOUND : vowel LETTER but consonant SOUND
Example : a European country / a university
4. The Importance of SOUND : consonant LETTER but vowel SOUND
Example : an honest man / an FBI agent