Dogs do not usually get hairballs because their tongues do not gather hair the same way a cat's does. It is possible he has swallowed something inappropriate and is choking, or he has some serious respiratory tract trouble. Either way, I do not think you can reasonably avoid a visit to the vet.
Can A Dog Have A Hairball Stuck In His Throat? He Is Gasping And Sounds Like He Is Unable To Get Something Like A Hairball Up.
Maybe you should check and see if he has downed a bone or something.
It may be, but it also may be something else. The only way to be for sure, is to take him to the vet.
I hate to say it, but it sounds like your dog (both of you) may have heartworms. Definitely time to see a vet.