Wearing a hat to play golf is not mandatory and is not usually part of the dress code either. However, you will see a huge amount of Tour Golf Professionals wearing baseball caps, as they get paid to do so, as part of their sponsorship contracts.