With constipation in fish, it is often caused by feeding too much dried or high protein foods- some types of goldfish are more prone to constipation than others but these are mostly the fancy types (in slim goldfish like comets and commons, the intestines are nicely laid out along the length of the guts in the fish, in fancy goldfish though they are compacted into a fat gut shape, making the fish more prone to constipation and swim bladder disorders).
To treat the constipation you should cut down on the amount of high protein and/or dried foods you are feeding the fish and make sure the water quality in the tank is excellant by doing lots of regular small water changes with water conditioner/dechlorinator. Replacing the goldfishes normal foods with cooked and de-shelled pea's (can be chopped up too) and/or blanched spinache will help get the fishes digestive tract back in good order and help clear any old food out that the fish is finding difficult to pass. The fish may not be as interested in the veg as they are in their normal foods, but cutting back on their normal foods should help incourage them to eat the veg

. Frozen/fresh (not dried) daphinia are also good for fishes digestion, you should be able to obtain it from your lfs/local fish store.
In bad cases of constipation though where it has been left untreated for a long time, the food in the fishes gut can start to rot and become a very serious problem, so in advanced cases of constipation you should probably use a med as well as the diet treatment like an anti internal bacterial one.
For treating internal bacteria, there are a lot of meds available for it depending on where you live in the world, me personally i prefer to use "Anti internal bacteria" by Interpet for treating internal bacteria- you should be able to get hold of it where you live as the Interpet range of meds is large and widespread and commonly available in most lfs in the UK

.
I would treat the fish for both internal bacteria and use the diet treatment (cutting down on dried/high protein foods, start feeding fish above mentioned veg, include frozen/fresh daphinia in the diet, keep tank very clean). Hopefully the fish is not on its last legs (or should i say fins?) and its condition is not too far progressed for the treatment to be of much help- the sooner you start treating the fish, the more chance of getting better and surviving it has

.