3 ms·
To me it seems like a problem induced by the way Go handles errors. With exceptions it would be something like try { // Some dangerous code whi
by adontz 4y ago
To me it seems like a problem induced by the way Go handles errors.
With exceptions it would be something like
try
{
// Some dangerous code which may throw/raise
w.Write([]byte("Hello, World!"))
}
catch( FileException )
{
w.Write([]byte("Screw your file!"))
}
catch( ... )
{
w.Write([]byte("Screw you in general!"))
}
and no such problem
- randomdata 4y agoThere is nothing special about errors, though. This same problem is present for any kind of branching operation. age := getUserAge() if age < 18 { w.Write([]byte("You are too young")) // Forgot to return here... } w.Write([]byte("<insert adult content>"))
- laumars 4y agoPutting aside discussions about Gos error handing and opinions about try and catch blocks, this specific problem (wrt the HTTP handler) can emulate the same code you’ve got above using a defer block. var err error defer func() { if err != nil { w.Write([]byte(err.Error()) } }()