You actually CAN breastfeed within 2 to 4 hours after surgery. The reason most doctors say to wait 24 hours is that they were trained before anyone ever studied anesthetics in breast milk. Well, this has now been researched and shown that the levels of anesthetic medication used in general anesthesia do not significantly persist in the breast milk beyond a couple hours. The little that does remain for the rest of the day is so minute, that it will have no noticeable effect on the baby.
My wife went through gallbladder surgery when our second son was a year old and still an avid nurser. Here is what she did. She pumped her breasts 4 hours after surgery and we threw this away. She then breastfed him on demand after that. Why “pump and dump” at four hours? There is really no good reason. We just did it to be extra safe. In reality, any medicine that gets into the milk during this time will move back out of the milk by four hours. But to make your doctor and the nurses happy, I suggest doing the 4-hour pump and dump.
Most pain medications you receive after surgery are also safe during breastfeeding, as a negligible amount of most of these medications makes it into the milk. Specific recommendations varies for different medications. As mentioned with anesthetics, your doctor may not be trained to handle this question well, so they may recommend you wait a certain amount of time before breastfeeding. If available, a lactation consultant will likely be a better source of advice.
For younger infants who will need to feed during or right after surgery, pump some milk beforehand to be fed through a bottle.