I used to keep goldfish and they got this every so often. The best thing to do is starve them for a couple of days I believe
edited to add - found this -
'The key to avoiding swim balder problems all together is to keep your fish on a varied diet. Which includes greens (not just peas) lettuce, spinach, duckweek, peas green beans, zucchini etc. Greens help keep the digestive tract of the goldfish clean. they have no stomachs like other fish and there fore need something to help push the foods thru their systems. Greens will do this. Adding these to their weekly even daily diet keeps them healthy.
Secondly, if you cannot get your fish to eat ONLY greens, do not even feed the other fish foods, give them all greens for a day or two and they will right themselves. If you cannot get him to eat yes you will need to use swimbladder meds, however I find they really don't work all that great.
Also, swimbladder is often confused with bloat. Bloat is caused by a number of things with goldfish mostly due to poor water conditions and salts in the water. The only salt that should ever be added to a goldfish tank is epsom salt. Never aquarium, table or marine salts. Salt interfears with all freshwater fish's ability to process water leading to all kinds of internal problems like bloat, dropsy and yes even swimbladder to name a few.
Try to get your fish to eat greens. Never feed tanked goldfish floating pellets, sinking or flakes in combination with fresh veggies and fruits. Even brine shrimp and bloodworms are a great addition.
Keep the water quality up, routine water changes feed veggies and fruits and quality flake foods.
He should show signs of improvement within a day'