I was listening to a sermon about how to be a man after God's heart. He was speaking about love, and he said David loved Saul even when Saul tried to kill him and searched for him for years. Even if Saul became David's enemy, David would never do the same against Saul. He loved Saul and he'd never become his enemy. Then he spoke about David's duel against Goliath, and David says, "You come against me with sword and spear and javelin, but I come against you in the name of the Lord Almighty, the God of the armies of Israel, whom you have defied ... today I will give the carcasses of the Philistine army to the birds of the air ... and the whole world will know that there is a God in Israel ... it is not by sword or spear that the Lord saves; for the battle is the Lord's, and he will give all of you into our hands."
Didn't David break the rule of loving your enemies? If you love a person you don't kill him or her, right?
David didn't count Goliath as a personal enemy. Goliath had chosen to be an enemy of Israel. David saw it as a manner of honor to stand bold for God. "What shall be done for the man who kills this Philistine and takes away the reproach from Israel? For who is this uncircumcised Philistine, that he should defy the armies of the living God?" (I Samuel 17:26). David was so certain that this was the right thing to do, that he volunteered to accept Goliath's challenge. "The LORD, who delivered me from the paw of the lion and from the paw of the bear, He will deliver me from the hand of this Philistine" (I Samuel 17:37).
You also need to see that Goliath was not an honorable man. A mere boy came out on the field to face him with no weapon of war and no armor. He did have a shield bearer, but that was about it. "And when the Philistine looked about and saw David, he disdained him; for he was only a youth, ruddy and good-looking. So the Philistine said to David, "Am I a dog, that you come to me with sticks?" And the Philistine cursed David by his gods. And the Philistine said to David, "Come to me, and I will give your flesh to the birds of the air and the beasts of the field!" Then David said to the Philistine, "You come to me with a sword, with a spear, and with a javelin. But I come to you in the name of the LORD of hosts, the God of the armies of Israel, whom you have defied. This day the LORD will deliver you into my hand, and I will strike you and take your head from you. And this day I will give the carcasses of the camp of the Philistines to the birds of the air and the wild beasts of the earth, that all the earth may know that there is a God in Israel. Then all this assembly shall know that the LORD does not save with sword and spear; for the battle is the LORD'S, and He will give you into our hands"" (I Samuel 17:42-47). An honorable man would have refused to attack a boy. But also see that this confrontation wasn't about David but about God.
"Oh, that You would slay the wicked, O God! Depart from me, therefore, you bloodthirsty men. For they speak against You wickedly; Your enemies take Your name in vain. Do I not hate them, O LORD, who hate You? And do I not loathe those who rise up against You? I hate them with perfect hatred; I count them my enemies" (Psalms 139:19-22).
When Jesus spoke about not hating your enemies, he was talking about taking personal vengeance. "But I say to you, love your enemies, bless those who curse you, do good to those who hate you, and pray for those who spitefully use you and persecute you, that you may be sons of your Father in heaven; for He makes His sun rise on the evil and on the good, and sends rain on the just and on the unjust" (Matthew 5:44-45). You see this in David, rarely did he attack those who attacked him. He would defend his countrymen and his God with his life, but refused to harm men like Saul, Absalom, or Shimei who attack him personally. "They reward me evil for good, to the sorrow of my soul. But as for me, when they were sick, my clothing was sackcloth; I humbled myself with fasting; and my prayer would return to my own heart. I paced about as though he were my friend or brother; I bowed down heavily, as one who mourns for his mother. But in my adversity they rejoiced and gathered together; attackers gathered against me, and I did not know it; they tore at me and did not cease" (Psalms 35:12-15).
Think about it this way. "For God so loved the world that He gave His only begotten Son, that whoever believes in Him should not perish but have everlasting life. For God did not send His Son into the world to condemn the world, but that the world through Him might be saved. He who believes in Him is not condemned; but he who does not believe is condemned already, because he has not believed in the name of the only begotten Son of God" (John 3:16-18). God loves everyone, even those who made themselves to be His enemy; yet, God's love does not preclude justice being served when there is a refusal to accept God's love. David's encounter with Goliath was a matter of justice and not a need for personal vengeance.